一種のダックタイピングをするために、私はそうします
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 クラス、関数の特殊化ではない、より洗練されたソリューションがあると思いますか (関数に余分なパラメーターを追加したくない)
私は次のようなことをしたいと思います
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' が宣言子として使用されています。
非型テンプレート パラメータの値によってテンプレート関数を特殊化する方法はありますか?