vendredi 3 juin 2016

C++ - Why is this function being called

What makes the second print function a better candidate than the first one? If non-templated functions have higher priority than templated ones, isn't both of this functions considered to be templated?

#include <iostream>
#include <type_traits>

template <typename T>
struct Base
{
    template <typename = typename std::enable_if<std::is_same<T, int>::value, bool>::type>
    void print(int a)
    {
        std::cerr << "Im print int: " << a << std::endl;
    }

    void print(T a)
    {
        std::cerr << "Im print T: " << a << std::endl;
    }
};

int main()
{
    Base<int> a;
    a.print(10);
    return 0;
}

Result: Im print T: 10

Aucun commentaire:

Enregistrer un commentaire