我偶然发现了我不明白的代码。这是它的简化版本:
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 产生以下错误:
$ 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
带和不带花括号的初始化应该是等效的。Clangi
正确初始化为42
,只是不认为它是编译时间常数。
此代码在 C++11 模式下编译良好。
是否有原因j
被视为编译时间常数而i
不是?或者它只是 Clang 中的一个错误?
更新:我在 LLVM 错误跟踪器中为这个问题打开了一张票。