Best practices include tagging because specialization is complex.
Sending tags is easier to use quite often:
template<typename T> T* only_if_int( std::true_type is_int ) { // code for T is int. // pass other variables that need to be changed/read above } T* only_if_int( std::false_type ) {return nullptr;} template<typename T> T* only_if_char( std::true_type is_char ) { // code for T is char. // pass other variables that need to be changed/read above } T* only_if_char( std::false_type ) {return nullptr;} template<typename T> T* function() { T* retval = only_if_int( std::is_same<T, int>() ); if (retval) return retval; retval = only_if_char( std::is_same<T, char>() ); return retval; }
source share