samedi 25 juillet 2015

SFINAE for class member function (one compiles the other not)

Why is class A compiling and class B not compiling, where the compiler complains about having two declarations, are not both relying on SFINAE? Both should actually use template type deduction when using foo?

So the question really is, whats the subtle different in these two version and why is class A successfully using sfinae...?

  • Class A uses a value-defaulted (zero) annonymous (not necessary) non-type template parameter
  • Class B uses a type-defaulted (with enable_if) annonymous type template parameter

The code:

template<typename T>
struct A
{

    template<typename U, 
             typename std::enable_if<!std::is_floating_point<U>::value>::type = 0
            >
    void foo() {}

    template<typename U, 
             typename std::enable_if<std::is_floating_point<U>::value>::type = 0
            >
    void foo() {}
};

template<typename T>
struct B
{

    template<typename U, 
             typename = typename std::enable_if<!std::is_floating_point<U>::value>::type
            >
    void foo() {}

    template<typename U, 
             typename = typename std::enable_if<std::is_floating_point<U>::value>::type
            >
    void foo() {}
};

Live code at: http://ift.tt/1Iu6LmE

Aucun commentaire:

Enregistrer un commentaire