有一种鸭子打字,我做
template<bool b>
struct A{
static template<typename V> f1(V*, [other params]);
static template<typename V> f2(V*, [other params]);
};
template<> template<typename T>
void A<false>::f1(V*, [other params]){}
template<> template<typename T>
void A<true>::f1(V*, [other params]){
...some code...
}
template<int flags>
struct V{
void f(){
A<flags&Some compile time conditions>::f1 (this,[params]);
A<flags&Some compile time conditions>::f2 (this,[params]);
}
};
你认为有没有更优雅的解决方案,不是模板类,函数特化 (我不想给函数添加额外的参数)
我想做类似的事情
template<int X> struct C{
void f(){std::cout<<"C::f"<<std::endl;};
};
template<> struct C<0>{
};
template<int X> struct D{
C<X> c;
template<bool b>
void f();
void g(){
f<X!=0>();
}
};
template<>
template<int X>
void D<X>::f<true>{
c.f();
};
template<int X>
template<>
void D<X>::f<false>{};
int main(){
D<3> ch;
ch.g();
D<0> cn;
cn.g();
}
但这不是有效的代码,我收到错误:template-id 'f' used as a declarator。
有没有办法通过其非类型模板参数的值来专门化模板函数?