I stumbled upon the code which I do not understand. Here's a simplified version of it:
template <int> struct A {};
int const i = { 42 };
typedef A<i> Ai;
int const j = 42;
typedef A<j> Aj;
This code compiles with GCC in C++98 mode, but not in Clang. Clang produces the following error:
$ 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 };
^
As far as I understand initialization of int
with and without curly braces should be equivalent. Clang initializes i
correctly to 42
, just doesn't think it's a compile time constant.
This code compiles well in C++11 mode.
Is there a reason j
is treated as a compile time constant and i
is not? Or is it simply a bug in Clang?
Update: I opened a ticket in LLVM bug tracker with this issue.
The compiler error states that
"template argument of type 'int' is not an integral constant expression"
forint const i = { 42 };
According to 98 standard the template argument should fall into this category:
and the definition of integral constant expression
int const i
fall into this category:and for the initialization of
i
(like Mike Seymour post):Now based on this post the declaration of
const int
andint const
should be the same (could not find this specifically in standard) makingi
a const variable. So any usage ofi
should be an integral constant expression regardless of the initialization method. It appears there is a bug in clang. Checking the web i could not find a bug report only two more or less similar:http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=666539
and
http://lists.cs.uiuc.edu/pipermail/llvmbugs/2011-March/017353.html