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.
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; };
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.
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 we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
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(); } };
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With