![](/img/trans.png)
[英]Statically asserting the size of a std::array whose type is obtained using decltype from the return value of a member function
[英]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.