C ++ function template overload by template parameter

Is it possible to overload the function template this way (only by the template parameter with enable_if):

template <class T, class = std::enable_if_t<std::is_arithmetic<T>::value>>
void fn(T t)
{

}
template <class T, class = std::enable_if_t<!std::is_arithmetic<T>::value>>
void fn(T t)
{

}

if conditions enable_ifdo not overlap? My MSVS compiler complains that 'void fn(T)' : function template has already been defined. If not, what is the alternative (ideally not placing enable_ifanywhere else except template parameters)?

+4
source share
1 answer

Arguments by default do not play a role in determining the uniqueness of functions. So the compiler sees that you define two functions, such as:

template <class T, class>
void fn(T t) { }

template <class T, class>
void fn(T t) { }

, , . enable_if :

template <class T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
void fn(T t) { }


template <class T, std::enable_if_t<!std::is_arithmetic<T>::value, int> = 0>
void fn(T t) { }

, , . SFINAE , .

+3

All Articles