can I prevent the generation of a function of a templated class if the template arguments does not meet a criteria (certain value or type)? Can this be achieved by using c++ code and no preprocessor directives?
This kind of function should neither be available in the header nor in the body. This case may seem a little artificial but i did not find a solution yet.
Example class (simplified - no constructor etc.):
MyClass.h
template<int Dimension, typename TPixelType = float>
class MyClass
{
void DoSomething();
void DoAnotherThing(); // this function should only be available if Dimension > 2
}
MyClass.cxx
template< int Dimension, typename TPixelType> void MyClass::DoSomething()
{...}
// pseudocode: if Dimension <= 2 do not compile next function
template< int Dimension, typename TPixelType> void MyClass::DoAnotherThing()
{
MethodNotToBeCompiled(); // The function I don't want to be executed
}
TestMyClass.cxx
int main()
{
auto myclass0 = MyClass<2>();
myclass0.DoSomething(); // OK
myclass0.DoAnotherThing(); // (wanted) error - function not available
auto myclass1 = MyClass<3>();
myclass1.DoSomething(); // OK
myclass1.DoAnotherThing(); // OK
}
Is this possible in C++XX? Or is there another approach than preprocessor directives?