Template Specialization for Basic Types

Is there a way to make template specialization only for basic types? I tried to do the following:

template<typename T, typename = typename std::enable_if<!std::is_fundamental<T>::value>::type> class foo { } template<typename T, typename = typename std::enable_if<std::is_fundamental<T>::value>::type> class foo { } 

But I get an error that the template is already defined.

+7
c ++ generic-programming templates metaprogramming
source share
1 answer

Here you create two template classes with the same name, not specialization.

You need to create a generic and then specialize it:

 // not specialized template (for non-fundamental types), Enabler will // be used to specialize for fundamental types template <class T, class Enabler = void> class foo { }; // specialization for fundamental types template <class T> class foo<T, std::enable_if_t<std::is_fundamental<T>::value>> { }; 
+15
source share

All Articles