使用constexpr函数作为模板参数有效吗
Is it valid to use constexpr function as template argument?
constexpr int get () { return 5; }
template<int N> struct Test {};
int main ()
{
int a[get()]; // ok
Test< get() > obj; // error:'int get()' cannot appear in a constant-expression
}
我用ideone编译了这个代码。我想知道为什么会出现编译错误。constexpr
函数是否不允许作为template
参数,或者它是编译器中的一个错误?
编辑:将const int get()
更改为int get()
此外,ideone还有一个错误,即如果删除constexpr
,则仍然允许声明数组!!我认为这是C99的特点。
GCC 4.5(至少是Ideone上使用的版本(并不完全支持constexpr
,包括您的有效用法;它下降到CCD_ 7。GCC 4.6及更高版本正确支持它。
相关文章:
- 我如何知道作为参数的size_t在函数中是否有效?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 为什么模板参数中的双冒号有效?
- 函数参数的名称与调用函数时使用的变量相同是否有效?
- 为什么使用不匹配的参数调用重载函数仍然有效
- C++/11 auto 关键字是在更有效时推导参数进行按引用传递,还是始终按值传递?
- 仅当一个参数中未使用 std::function 时,模板函数替换才有效
- 自动模板类enable_if有效参数
- 带有"up-"和"center-vec3"参数的glm::rotate()的有效替代方案?
- 为什么重写方法并将 const 添加到参数类型有效
- 字符串或const char*,更有效地用作构造函数参数
- 在 pthread_create() 中使用局部变量作为参数有效吗?
- 如何有效地传递 std::vector<cv::P oint3f> 的子向量作为参数(不拥有函数)
- 作为模板非类型参数的 C 字符串在 gcc 6.3 中有效,但在 Visual Studio 2017(x64 为 19
- 如果 null 不是有效值,则使参数成为引用而不是指针
- Qt C++ - 创建 QList<Myclass> - 不是参数 'T 的有效模板类型参数
- 如何以有效的方式将输入参数传递给函数
- C++17 有效地将参数包参数与 std::array 元素相乘
- GLSL 错误:"out"限定符仅对 GLSL 1.10 中的函数参数有效
- 使用constexpr函数作为模板参数有效吗