So in a template class, I was trying to instantiate different member according to some information provided statically. In the below template, if some condition holds, the selectiveMember
is contained within SelectiveClass
, otherwise, the template is intended to be instantiated without selectiveMember
but still contain foo
.
template<typename T>
struct SelectiveClass{
void foo(){...}
template<condition_check<T>::type=0>
void selectiveMember{...}
}
However, this kind of implementation would discard the whole class if condition is not satisfied. Nevertheless, using the CRTP technique, we can reach the purpose.
template<typename T>
struct SelectiveClass: Specialize<T>{
void foo(){...}
}
template<typename T>
struct Specialize{
template<condition_check<T>::type=0>
void selectiveMember{...}
}
But with this technique, each selective member would require another specialized inheritance.
So my question is: Is there any more elegant way to allow this kind of selective member template?