简体   繁体   English

g ++ 8.1模板推导abmiguity,std标志等于c ++ 17第2部分

[英]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. 我问了这个问题并得到了一个很好的答案,但是在g ++中使用c ++ 17标志还有一个关于模板参数推导的不明确点。

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. 第一个重载比第二个更专业:如果我们传递类型为std::vector<P1,P2>的参数,则可以推导出第二个重载的模板参数T,A,Vector ,其中P1和P2是两个发明类型。

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 . 此处描述了模板功能的排名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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