Wednesday, May 23, 2012

Calling constexpr in default template argument

In C++11 I am using a constexpr function as a default value for a template parameter - it looks like this:



template <int value>
struct bar
{
static constexpr int get()
{
return value;
}
};

template <typename A, int value = A::get()>
struct foo
{
};

int main()
{
typedef foo<bar<0>> type;

return 0;
}


G++ 4.5 and 4.7 compiles this, but Clang++ 3.1 does not. The error message from clang is:



clang_test.cpp:10:35: error: non-type template argument is not a constant expression
template <typename A, int value = A::get()>
^~~~~~~~
clang_test.cpp:17:19: note: while checking a default template argument used here
typedef foo<bar<3>> type;
~~~~~~~~~^~
clang_test.cpp:10:35: note: undefined function 'get' cannot be used in a constant expression
template <typename A, int value = A::get()>
^
clang_test.cpp:4:23: note: declared here
static constexpr int get()
^
1 error generated.


Which one is correct?





No comments:

Post a Comment