How does std::enable_if work when toggling functions?

73 Views Asked by At

I read about std::enable_if at cppreference.

It describes right and wrong way of using it.

/* 1st Example : WRONG */
 
struct T
{
    enum { int_t, float_t } type;
 
    template<typename Integer,
             typename = std::enable_if_t<std::is_integral<Integer>::value>>
    T(Integer) : type(int_t) {}
 
    template<typename Floating,
             typename = std::enable_if_t<std::is_floating_point<Floating>::value>>
    T(Floating) : type(float_t) {} // error: treated as redefinition
};
 
/* 2nd Example : RIGHT */
 
struct T
{
    enum { int_t, float_t } type;
 
    template<typename Integer,
             std::enable_if_t<std::is_integral<Integer>::value, bool> = true>
    T(Integer) : type(int_t) {}
 
    template<typename Floating,
             std::enable_if_t<std::is_floating_point<Floating>::value, bool> = true>
    T(Floating) : type(float_t) {} // OK
};

But I couldn't understand why the second example works.

In the first example even if default argument of template is different, it is not treated as different template function. So compiler generates error of re-definition of function. This is my understaing.

But the second example also looks similar case as the first example to me. How does the second example works?

0

There are 0 best solutions below