简体   繁体   中英

g++ 8.1 template deduction abmiguity with std flag equal to c++17 part 2

I asked this question and got a good answer but there is a one more not clear point about template parameter deduction in g++ with c++17 flag.

If we will take this code:

#include <iostream>
#include <vector>

template<class T, class A>
void func(const std::vector<T, A>&v)
{
    std::cout << 1 << std::endl;
}

template<typename T, typename A, template <typename, typename>class Vector>
void func(const Vector<T, A>&v)
{
    std::cout << 2 << std::endl;
}

void f()
{
    std::vector<int> v;
    func(v);
}

int main()
{
    f();
    return 0;
}

Difference in declaration of second template function. In this case accordingly the answer template parameter deduction should be the same as before. But compiler not report about any ambiguity.

Previous version of second function which produce ambiguity error:

template<typename T, template <typename>class Vector>
void func(const Vector<T>&v)
{
    std::cout << 2 << std::endl;
}

What I'm missing in this case?

With this new set of overloaded functions:

template<class T, class A>
void func(const std::vector<T, A>&v)

template<typename T, typename A, template <typename, typename>class Vector>
void func(const Vector<T, A>&v);

The first overload is more specialized than the second: the template parameters T,A,Vector of the second overload can be deduced if we passed an argument of type std::vector<P1,P2> where P1 and P2 are two invented types.

While with the previous set of overload:

template<class T, class A>
void func(const std::vector<T, A>&v);

template<typename T, template <typename>class Vector>
void func(const Vector<T>&v);

Neither of the two overloads are more specialized than the other. Because the previously described template argument deduction is not realizable.

The ranking of template function is described here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM