简体   繁体   English

在没有 function 调用的情况下确定 std::array 返回类型的大小

[英]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.我有一个 class B ,它将A之类的类作为模板参数。

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

Each T has an operator()() that returns an std::array<double, N> .每个T都有一个operator()() ,它返回一个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()() .我希望每个专业化B<T>能够推断N而不需要对T s 有额外的要求,也不需要调用operator()() How can I do this?我怎样才能做到这一点?

An example T is below and labelled as class A :下面是一个示例T并标记为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您可以将成员添加到B ,就像这样

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

Here's a demo这是一个演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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