Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::enable_if : parameter vs template parameter

I'm building some input checker that needs to have specific functions for integer and/or double (for example 'isPrime' should only be available for integers).

If I'm using enable_if as a parameter it's working perfectly :

template <class T> class check { public:    template< class U = T>    inline static U readVal(typename std::enable_if<std::is_same<U, int>::value >::type* = 0)    {       return BuffCheck.getInt();    }     template< class U = T>    inline static U readVal(typename std::enable_if<std::is_same<U, double>::value >::type* = 0)    {       return BuffCheck.getDouble();    }    }; 

but if I'm using it as a template paramater (as demonstrated on http://en.cppreference.com/w/cpp/types/enable_if )

template <class T> class check { public:    template< class U = T, class = typename std::enable_if<std::is_same<U, int>::value>::type >    inline static U readVal()    {       return BuffCheck.getInt();    }     template< class U = T, class = typename std::enable_if<std::is_same<U, double>::value>::type >    inline static U readVal()    {       return BuffCheck.getDouble();    } }; 

then I have the following error :

error: ‘template<class T> template<class U, class> static U check::readVal()’ cannot be overloaded error: with ‘template<class T> template<class U, class> static U check::readVal()’ 

I can't figure out what is wrong in the second version.

like image 405
Loïc Février Avatar asked Jun 15 '12 18:06

Loïc Février


People also ask

What is STD Enable_if?

std::enable_if is a convenient utility to use boolean conditions to trigger SFINAE. It is defined as: template <bool Cond, typename Result=void> struct enable_if { }; template <typename Result> struct enable_if<true, Result> { using type = Result; };

Can we pass Nontype parameters to templates?

Template non-type arguments in C++It is also possible to use non-type arguments (basic/derived data types) i.e., in addition to the type argument T, it can also use other arguments such as strings, function names, constant expressions, and built-in data types.

What is Enable_if C++?

The type T is enabled as member type enable_if::type if Cond is true. Otherwise, enable_if::type is not defined. This is useful to hide signatures on compile time when a particular condition is not met, since in this case, the member enable_if::type will not be defined and attempting to compile using it should fail.

Why do we use template template parameter?

Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.


1 Answers

Default template arguments are not part of the signature of a template (so both definitions try to define the same template twice). Their parameter types are part of the signature, however. So you can do

template <class T> class check { public:    template< class U = T,               typename std::enable_if<std::is_same<U, int>::value, int>::type = 0>    inline static U readVal()    {       return BuffCheck.getInt();    }     template< class U = T,               typename std::enable_if<std::is_same<U, double>::value, int>::type = 0>    inline static U readVal()    {       return BuffCheck.getDouble();    } }; 
like image 149
Johannes Schaub - litb Avatar answered Oct 02 '22 16:10

Johannes Schaub - litb