【发布时间】:2020-09-01 10:30:25
【问题描述】:
我有一个带有成员函数的类,它声明一个数组,其大小基于公式。
template <int SIZE>
class Example{
constexpr int lookup(const int n) const
{
return n * n + n;
}
inline void func()
{
double array[lookup(SIZE)];
}
};
这给了我 vla 错误。我认为它应该可以工作,因为SIZE 在编译时被解析,并且查找是constexpr。我知道以下方法会起作用:
template <int SIZE>
class Example{
inline void func()
{
constexpr int sz = SIZE * SIZE + SIZE;
double array[sz];
}
};
我想我只是想弄清楚为什么
编辑抱歉打错了,试图写一个更小的例子,结果缺少n和类名。
【问题讨论】:
-
n来自哪里? -
通过一些小的语法修复,它可以正常运行:repl.it/repls/WanLavishComputergames
-
@BillLynch - 编译和运行良好,但不是标准 C++;如果添加“-pedantic”,则会收到警告“警告:可变长度数组是 C99 功能”。
标签: c++ arrays c++11 template-meta-programming constexpr