Search code examples
c++templatesdecltypestdarraytype-deduction

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 Ts 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;
    }
};

Solution

  • 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