模板模板参数和默认参数问题

模板模板参数和默认参数问题

本文介绍了C ++ 17中的歧义错误(模板模板参数和默认参数问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我有一些代码,这些代码由g ++用c++14c++17标准标志来解释:

#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, template <typename>class Vector>
void func(const Vector<T>&v)
{
    std::cout << 2 << std::endl;
}

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

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

当我尝试使用命令编译此代码

一切正常.

但是当我尝试使用命令编译此代码时

我收到此错误:

main.cpp: In function 'void f()':
main.cpp:19:11: error: call of overloaded 'func(std::vector<int>&)' is ambiguous
     func(v);
           ^
main.cpp:5:6: note: candidate: 'void func(const std::vector<_Tp, _Alloc>&) [with T = int; A = std::allocator<int>]'
 void func(const std::vector<T, A>&v)
      ^~~~
main.cpp:11:6: note: candidate: 'void func(const Vector<T>&) [with T = int; Vector = std::vector]'
 void func(const Vector<T>&v)

从C ++ 17标准的角度来看,我无法弄清楚这段代码有什么问题.

解决方案

自C ++ 17以来,行为已更改.

在C ++ 17之前,该代码有效,因为 std::vector 有两个模板参数(第二个具有默认参数std::allocator<T>),而模板模板参数Vector被声明为只有一个,它们不匹配,因此将不考虑第二个func. /p>

从C ++ 17开始( CWG 150 ),则允许模板模板参数使用默认模板参数将模板模板参数与更少的模板参数进行匹配.这意味着func都将成为有效候选人,然后导致模棱两可.

I have code which is differently interpreted by g++ with the c++14 and c++17 standard flags:

#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, template <typename>class Vector>
void func(const Vector<T>&v)
{
    std::cout << 2 << std::endl;
}

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

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

When I'm trying compile this code with command

everything works just fine.

But when I'm trying to compile this code with command

I get this error:

main.cpp: In function 'void f()':
main.cpp:19:11: error: call of overloaded 'func(std::vector<int>&)' is ambiguous
     func(v);
           ^
main.cpp:5:6: note: candidate: 'void func(const std::vector<_Tp, _Alloc>&) [with T = int; A = std::allocator<int>]'
 void func(const std::vector<T, A>&v)
      ^~~~
main.cpp:11:6: note: candidate: 'void func(const Vector<T>&) [with T = int; Vector = std::vector]'
 void func(const Vector<T>&v)

I can't figure out what is wrong with this code from the C++17 standard's point of view.

解决方案

The behavior changed since C++17.

Before C++17, the code works because std::vector has two template parameters (the 2nd one has the default argument std::allocator<T>), while the template template parameter Vector is declared to have only one, they don't match then the 2nd func won't be considered.

Since C++17 (CWG 150), the default template arguments are allowed for a template template argument to match a template template parameter with fewer template parameters. That means both func become valid candidates and then leads to ambiguity.

这篇关于C ++ 17中的歧义错误(模板模板参数和默认参数问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 09:17