【发布时间】:2012-05-23 13:36:39
【问题描述】:
在 C++11 中,我使用 constexpr 函数作为模板参数的默认值 - 它看起来像这样:
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 和 4.7 编译这个,但 Clang++ 3.1 没有。来自 clang 的错误信息是:
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.
哪个是正确的?
【问题讨论】:
-
我对 clang 3.1 有同样的问题。关于如何解决它的任何想法?我是否必须使用模板元编程而不是 constexpr,才能在模板实例化中“调用”它?
标签: c++ gcc c++11 clang constexpr