Kla*_*aus 6 c++ templates template-specialization c++14 template-variables
一个类可以包含一个必须是静态的成员模板变量:
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; }
};
Run Code Online (Sandbox Code Playgroud)
它必须在类范围之外声明所有静态成员:
以下编译并按预期工作:
template<typename T> T B::var=9; // makes only sense for int,float,double...
Run Code Online (Sandbox Code Playgroud)
但是如何将这样的var专门化为以下非工作代码(使用gcc 6.1的错误消息):
template <> double B::var<double>=1.123;
Run Code Online (Sandbox Code Playgroud)
失败:
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;
Run Code Online (Sandbox Code Playgroud)
template <> double B::var=1.123;
Run Code Online (Sandbox Code Playgroud)
失败了
template <> double B::var=1.123;
^~~
main.cpp:38:22: note: does not match member template declaration here
static X var;
Run Code Online (Sandbox Code Playgroud)
这里的语法是什么?
我想你应该添加一个空格
template <> double B::var<double> = 1.123;
^ here
Run Code Online (Sandbox Code Playgroud)
否则(如果我没有错)>=1.123与"等于或大于1.123"相混淆
| 归档时间: |
|
| 查看次数: |
172 次 |
| 最近记录: |