【发布时间】:2015-11-10 01:46:55
【问题描述】:
拥有:
struct Value
{
template<class T>
static constexpr T value{0};
};
(0) ideone
template<typename TValue>
struct Something
{
void x()
{
static_assert(TValue::template value<int> == 0, "");
}
};
int main() { Something<Value>{}.x(); return 0; }
-
不能用 clang++ 3.6 编译。
错误:不能在没有模板参数列表的情况下引用变量模板“值”
-
不使用 g++ 5.2 编译。
错误:'template constexpr const T Value::value' 不是函数模板
(1) ideone
同时使用 clang++ 和 g++ 编译。
struct Something
{
void x()
{
static_assert(Value::template value<int> == 0, "");
}
};
int main() { Something{}.x(); return 0; }
为什么(0)编译失败?
如果通过模板参数(在本例中为TValue)访问变量模板,似乎会出现问题。为TValue 定义类型别名或使用typename 关键字并不能解决问题。
这里发生了什么?
【问题讨论】:
-
template<class T> static constexpr T value{0};应该做什么?是新事物吗?怎么称呼? -
(0) 在 clang 3.6 上对我来说失败,“无法在没有模板参数列表的情况下引用变量模板 '
value'” -
我编辑了原始问题:
auto不是问题的一部分 - 由于某种原因,当使用int代替auto时,ClangComplete(Sublime Text 插件)没有显示错误。问题在于TValue是Something的模板参数。
标签: c++ templates c++14 auto variable-templates