要有一种鸭子类型的,我做鸭打字C++(通过其非类型模板参数的值专门的模板功能)
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 class, function specialization (我不想额外PARAM添加到功能)
我想这样做
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'。
有没有办法通过其非类型模板参数的值专门化模板函数?
这可能有助于在main()中给出如何使用D的示例。 –
对不起,主要是我打算使用D.C仅用于做出“可能存在或不存在在那里“ –