【发布时间】:2012-07-11 09:02:46
【问题描述】:
我正在尝试创建一个从另一个模板类(此处为 A)继承的模板类(此处为 C)并执行静态成员特化(此处为 int var),但我无法获得正确的语法(如果有可能
#include <iostream>
template<typename derived>
class A
{
public:
static int var;
};
//This one works fine
class B
:public A<B>
{
public:
B()
{
std::cout << var << std::endl;
}
};
template<>
int A<B>::var = 9;
//This one doesn't works
template<typename type>
class C
:public A<C<type> >
{
public:
C()
{
std::cout << var << std::endl;
}
};
//template<>
template<typename type>
int A<C<type> >::var = 10;
int main()
{
B b;
C<int> c;
return 0;
}
我举了一个与非模板类(此处为 B)一起使用的示例,并且我可以获得 var 的静态成员特化,但对于 C 则不起作用。
这是 gcc 告诉我的:
test.cpp: In constructor ‘C<type>::C()’:
test.cpp:29:26: error: ‘var’ was not declared in this scope
test.cpp: At global scope:
test.cpp:34:18: error: template definition of non-template ‘int A<C<type> >::a’
我使用的是 gcc 版本 4.6.3,感谢您的帮助
【问题讨论】:
标签: c++ templates static specialization