英特尔编译器和使用 constexpr 时的"cannot have an in-class initializer"

Intel compiler and "cannot have an in-class initializer" when using constexpr

本文关键字:cannot have in-class initializer an 时的 编译器 constexpr 英特尔      更新时间:2023-10-16

下面的测试程序在g++中可以很好地编译和运行。在Intel icpc(14.0.2)中,如果我使用double这样的显式类型而不是模板,它将编译和运行。使用icpc的模板版本产生一个错误:

icpc -g -O2 -I. -std=c++0x -c main.cc -o main.o
main.cc(10): error: a member of type "const T [9]" cannot have an in-class initializer
    static constexpr T dx_[9] = {
<<p> 测试代码/strong>
template<typename T>
class myclass {
public:
    static constexpr T dx_[9] = {
         1.5,  2.0, -0.5,
        -0.5,  0.0,  0.5,
         0.5, -2.0, -1.5
        };
};
template<typename T> constexpr T myclass<T>::dx_[9];
int main(int argc, char *argv[]) {
    return 0;
} // main

为什么我收到错误"不能有一个类内初始化器"当使用constexpr ?

这是英特尔编译器的错误,已提交给英特尔,将在未来的版本中修复。

参见intel论坛上的多个constexpr bug、c++编译器15中的sfinae bug和c++编译器15中的方法constexpr bug。

看来你的编译器过时了。-std=c++0x标志表明它早在c++ 11标准实现之前就实现了。

尝试使用-std=c++11开关,如果你的编译器支持它。

否则,请升级编译器或不要使用这些花哨的新功能。