说我有以下课程:
template<class T>
struct A
{
static int value;
};
template<class T>
int A<T>::value = 0;
Run Code Online (Sandbox Code Playgroud)
我可以专注A::value于一个没有问题的具体类型:
struct B
{
};
template<>
int A<B>::value = 1;
Run Code Online (Sandbox Code Playgroud)
我想在模板类型上专门化A ::值,我尝试了以下方法:
template<class T>
struct C
{
};
// error: template definition of non-template 'int A<C<T> >::value'
template<>
template<class T>
int A<C<T> >::value = 2;
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一点,或者只能在非模板类型上专门化A ::值?
您可以专门初始化,而不是引入完整的显式特化
template<class T>
struct Value {
static int const value = 0;
};
template<class T>
struct Value< C<T> > {
static int const value = 2;
};
template<class T>
int A<T>::value = Value<T>::value;
Run Code Online (Sandbox Code Playgroud)