[英]specialization of static method template in class template
is there a way to make this code work? 有没有办法让这个代码工作?
template<typename T> class test{
public:
template<typename T2> static void f(){
cout<<"generic"<<endl;
}
template<> static void f<void>(){ //specialization of static method for template T2, within generic class template with argument T
cout<<"void"<<endl;
}
};
if not, is it because it counts as a partial template specialization of a function? 如果不是,是因为它算作函数的部分模板特化吗?
As others have pointed out in the comments and link, the current C++ template syntax provides no easy way to support this usage. 正如其他人在评论和链接中指出的那样,当前的C ++模板语法没有提供支持这种用法的简单方法。 However, if you really want to do this and you don't mind introducing some complexity read on.
但是,如果你真的想这样做,你不介意介绍一些复杂性。
The two main problems you have to deal with are: 您必须处理的两个主要问题是:
To get around them and get close to what you're looking for here's what you can try. 为了绕过它们并接近你正在寻找的东西,你可以尝试。
template<typename T>
class test
{
public:
template <typename T2>
static void f();
};
template <typename T1, typename T2>
struct f
{
void operator ()(void) { cout << "generic" << endl; }
};
template <typename T1>
struct f<T1, void>
{
void operator ()(void) { cout << "void" << endl; }
};
template <typename T>
template <typename T2>
void test<T>::f()
{
::f<T, T2>()();
}
int main()
{
test<int>::f<int>(); // prints 'generic'
test<int>::f<void>(); // prints 'void'
}
That's an awful lot of extra code introduced for something like this but I suppose if you want to do this bad enough it's a possibility. 这是为这样的事情引入的大量额外代码,但我想如果你想要做到这么糟糕,这是一种可能性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.