如何解决根据 const 类成员的大小初始化 stdarray
有人可以向我解释为什么以下会导致错误“非类型模板参数不是常量表达式”以及如何处理?
#include <array>
class Test
{
public:
Test(int n) : num_requests(n/2){};
const int num_requests;
void func()
{
std::array <int,num_requests> test_array;
};
};
解决方法
使用模板“参数”like this:
#include <array>
template <int n>
struct Test
{
void func()
{
std::array <int,n/2> test_array;
};
};
int main() {
auto t = Test<10>{};
t.func();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。