繁体   English   中英

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

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

我有一个 class B ,它将A之类的类作为模板参数。

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

每个T都有一个operator()() ,它返回一个std::array<double, N> 我希望每个专业化B<T>能够推断N而不需要对T s 有额外的要求,也不需要调用operator()() 我怎样才能做到这一点?

下面是一个示例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;
    }
};

您可以将成员添加到B ,就像这样

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

这是一个演示

暂无
暂无

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

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