0

I would like to change my function definition based on type by using enable_if_t. Some thing similar to this:

#include<type_traits>
template<typename T> struct A;

template<typename T>
struct A{
    std::enable_if_t<std::is_arithmetic<T>::value, bool>
    test()
    {
        return true;
    }
    std::enable_if_t<!std::is_arithmetic<T>::value,  bool>
    test()
    {
        return false;    
    }

};

Currently it cannot compile at all.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Wang
  • 7,250
  • 4
  • 35
  • 66
  • 1
    Class methods with the same name and signature are not allowed. A function's return type is not a part of its signature. The easiest solution here is to declare the return type as `auto`, and use `if constexpr` to return the appropriate type. – Sam Varshavchik Jun 09 '20 at 17:32

1 Answers1

0

You can try with

template <typename U = T>
std::enable_if_t<std::is_arithmetic<U>::value, bool>
test()
 { return true; }

template <typename U = T>
std::enable_if_t<!std::is_arithmetic<U>::value,  bool>
test()
 { return false; }

I mean... SFINAE works over template parameter of the functions/methods, not over template parameters of the class containing the methods.

With typename U = T you transform T, the class template parameter, in U, a method template parameter.

max66
  • 65,235
  • 10
  • 71
  • 111