【发布时间】:2019-01-24 08:45:19
【问题描述】:
我的代码使用 c++14 和 c++17 标准标志被 g++ 以不同方式解释:
#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;
}
当我尝试使用命令编译此代码时
g++ -std=c++14 -Wall -pedantic main.cpp
一切正常。
但是当我试图用命令编译这段代码时
g++ -std=c++17 -Wall -pedantic main.cpp
我收到此错误:
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++ templates c++14 language-lawyer c++17