[英]Static template member function for template class
I have a template class and a template member function:我有一个模板类和一个模板成员函数:
template<class T1>
struct A{
template<class T2>
static int f(){return 0;}
};
I want to specialize for a case when T1
and T2
are the same,我想专攻
T1
和T2
相同的情况,
For example, define the case A<T>::f<T>
for any T
.例如,为任何
T
定义 case A<T>::f<T>
。
but I can't find the combination of keywords to achieve this.但我找不到关键字组合来实现这一点。
How can I partially (?) specialize a combination of template class and a template static function?我如何部分(?)专门化模板类和模板静态函数的组合?
These are my unsuccessful attempts, and the error messages:这些是我失败的尝试,以及错误消息:
1) Specialize inside the class: fatal error: cannot specialize a function 'f' within class scope
) 1) 在类
fatal error: cannot specialize a function 'f' within class scope
: fatal error: cannot specialize a function 'f' within class scope
)
template<class T1>
struct A{
template<class T2>
static int f(){return 0;}
template<>
static int f<T1>(){return 1;}
};
2) Specialize outside the class "simultaneously": fatal error: cannot specialize a member of an unspecialized template
2) 在类外“同时”特化:
fatal error: cannot specialize a member of an unspecialized template
template<class T>
void A<T>::f<T>(){return 1;}
3) Specialize using template<>
: fatal error: too few template parameters in template redeclaration
3) 专门使用
template<>
: fatal error: too few template parameters in template redeclaration
template<> template<class T>
void A<T>::f<T>(){return 1;}
4) Invert the order: fatal error: cannot specialize (with 'template<>') a member of an unspecialized template
4) 颠倒顺序:
fatal error: cannot specialize (with 'template<>') a member of an unspecialized template
template<class T> template<>
void A<T>::f<T>(){return 1;}
5) Specialize the whole class (based on the error of attempt 3): fatal error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
5)特化整个类(基于尝试3的错误):
fatal error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
fatal error: class template partial specialization does not specialize any template argument; to define the primary template, remove the template argument list
template<class T>
struct A<T>{
template<>
static int f<T>(){return 1;}
};
6) Specialize the class but not the function (?): fatal error: too few template parameters in template redeclaration template<> template<class T1>
6)特化类而不特化函数(?):
fatal error: too few template parameters in template redeclaration template<> template<class T1>
template<> template<class T>
int A<T>::f(){return 0;}
I used clang 3.5 C++14
to generate the error messages.我使用了
clang 3.5 C++14
来生成错误消息。
You cannot partially specialize a function.你不能部分特化一个函数。
You can forward the work to another type:您可以将工作转发到另一种类型:
template<class T1>
struct A;
template<class T1, class T2>
struct A_helper {
static int f() {
return A<T1>::f_simple<T2>();
}
};
template<class T>
struct A_helper<T,T> {
static int f() {
return A<T>::f_special();
}
};
template<class T1>
struct A{
template<class T2>
static int f(){return A_helper<T1,T2>::f()}
template<class T2>
static int f_simple(){return 0;}
static int f_special(){return -1;}
};
or, more simply, with no A_helper
:或者,更简单地说,没有
A_helper
:
template<class T1>
struct A{
template<class T2>
static int f(){return f2<T2>(std::is_same<T1,T2>{});}
template<class T2>
static int f2(std::true_type){
static_assert(std::is_same<T1,T2>{}, "bob!");
return -1;
}
template<class T2>
static int f2(std::false_type){return -1;}
};
you can use tag dispatching.您可以使用标签调度。
Another approach:另一种方法:
template<class T>struct tag{};
template<class T1>
struct A{
template<class T2>
static int f(){return f2(tag<T2>{});}
template<class T2>
static int f2(tag<T2>) {return 0;}
static int f2(tag<T1>) {return -1;}
};
where we directly dispatch on the type of T2
.我们直接调度
T2
的类型。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.