问题描述
有人可以向我解释为什么以下会导致错误“非类型模板参数不是常量表达式”以及如何处理?
#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();
}