[英]member template variable specializing
A class can contain a member template variable which must be static: 一个类可以包含一个必须是静态的成员模板变量:
class B
{
public:
template <typename X>
static X var;
B() { std::cout << "Create B " << __PRETTY_FUNCTION__ << std::endl; }
template <typename T>
void Print() { std::cout << "Value is " << var<T> << std::endl; }
};
It must as all static members be declared outside the class scope: 它必须在类范围之外声明所有静态成员:
The following compiles and works as expected: 以下编译并按预期工作:
template<typename T> T B::var=9; // makes only sense for int,float,double...
But how to specialize such a var like the following non working code ( error messages with gcc 6.1): 但是如何将这样的var专门化为以下非工作代码(使用gcc 6.1的错误消息):
template <> double B::var<double>=1.123;
Fails with: 失败:
main.cpp:49:23: error: parse error in template argument list
template <> double B::var<double>= 1.123;
^~~~~~~~~~~~~~~~~~
main.cpp:49:23: error: template argument 1 is invalid
main.cpp:49:23: error: template-id 'var<<expression error> >' for 'B::var' does not match any template declaration
main.cpp:38:22: note: candidate is: template<class X> T B::var<T>
static X var;
template <> double B::var=1.123;
Fails with 失败了
template <> double B::var=1.123;
^~~
main.cpp:38:22: note: does not match member template declaration here
static X var;
What is the correct syntax here? 这里的语法是什么?
I suppose you should add a space 我想你应该添加一个空格
template <> double B::var<double> = 1.123;
^ here
Otherwise (if I'm not wrong) >=1.123
is confused with "equal or greather than 1.123" 否则(如果我没错)
>=1.123
与“等于或大于1.123”相混淆
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.