よくわからないコードに出くわしました。これを簡略化したバージョンを次に示します。
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 バグ トラッカーでチケットをオープンしました。