我偶然发现了我不明白的代码。下面是它的一个简化版本:C++ 98大括号const标量初始化
template <int> struct A {};
int const i = { 42 };
typedef A<i> Ai;
int const j = 42;
typedef A<j> Aj;
此代码在C++ 98模式下编译为GCC,但不在Clang中编译。锵产生以下错误:
$ clang -Wall -Wextra -std=c++98 -c test.cpp
test.cpp:4:11: error: non-type template argument of type 'int' is not an integral constant expression
typedef A<i> Ai;
^
test.cpp:4:11: note: initializer of 'i' is not a constant expression
test.cpp:3:11: note: declared here
int const i = { 42 };
^
据我了解有和没有大括号应该是相当于int
初始化。 Clang将i
初始化为42
,但并不认为这是编译时间常量。
此代码在C++ 11模式下编译良好。
是否有原因j
被视为编译时间常量,i
是不是?或者它仅仅是Clang中的一个错误?
更新:我在LLVM bug跟踪器中打开了一个ticket这个问题。
我会说这是在编译器中的错误。 – Raxvan
@Raxvan:这是一个非常大胆的陈述......考虑到它在C++ 11中工作,而不是在C++ 98中,它似乎是非常慎重的,所以我实际上期望它是*按规格*(在Clang部分) ,并且gcc像往常一样更宽松。 –
@MatthieuM .:我认为这是他想说的:它是一个gcc中的bug。没有C++ 98的语法,clang正确地拒绝它。所以接受它在gcc中,尽管不是C++ 98,是一个错误。 – PlasmaHH