This is done through partial specialization. The coarsest version looks like this:
template <typename, bool> class superclass1; template <class param> class superclass1<param, true> class superclass1 { public: int foo() { doSomthingMore(); return 1; } }; template <class param> class superclass1<param, false> class superclass1 { public: int foo() { return 1; } };
A more sophisticated approach is to declare a member template function and only specialize in this. Here's a solution with helper tag classes:
#include <type_traits> template <bool B> class Foo { struct true_tag {}; struct false_tag {}; void f_impl(true_tag = true_tag()){} // your code here... void f_impl(false_tag = false_tag()){} // ... and here public: void foo() { f(typename std::conditional<B, true_tag, false_tag>::type()); } };
source share