简体   繁体   中英

Determine size of std::array return type without a function call

I have a class B that takes classes like A as a template parameter.

template<typename T>
class B{
///...

Each T has an operator()() that returns an std::array<double, N> . I would like each specialization B<T> to be able to deduce N without additional requirement on the T s and without calling the operator()() . How can I do this?

An example T is below and labelled as class A :

template <int N>
class A {
public:
    A() {}

    std::array<double, N> operator()() {
        std::array<double, N> the_integers;
        for (int i = 0; i < N; ++i) {
            the_integers[i] = i;
        }
        return the_integers;
    }
};

You can add a member to B , like this

static constexpr std::size_t ArraySize = std::tuple_size_v<decltype(std::declval<T&>()())>;

Here's a demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM