[英]Why is this call to the overloaded function ambiguous?
考虑这个程序-
#include <string>
#include <vector>
#include <set>
void fun(const std::string& val) {
}
void fun(std::vector<std::string> val) {
}
int main()
{
std::set<std::string> example;
fun({std::begin(example), std::end(example)});
}
在编译时,我遇到了这些错误-
prog.cc: In function 'int main()':
prog.cc:13:49: error: call of overloaded 'fun(<brace-enclosed initializer list>)' is ambiguous
13 | fun({std::begin(example), std::end(example)});
| ^
prog.cc:4:6: note: candidate: 'void fun(const string&)'
4 | void fun(const std::string& val) {
| ^~~
prog.cc:7:6: note: candidate: 'void fun(std::vector<std::__cxx11::basic_string<char> >)'
7 | void fun(std::vector<std::string> val) {
| ^~~
我知道std::string
有一个构造函数重载,它接受一个initializer_list
像这样-
basic_string( std::initializer_list<char> ilist,
const Allocator& alloc = Allocator() );
和std::vector<std::string>
有一个看起来像这样的重载-
vector( std::initializer_list<std::string> init,
const Allocator& alloc = Allocator() );
因此,很明显这两种方法的类型不同。 一个接受char
的initializer_list
,另一个接受std::string
类型。
在我的代码中,当我传递字符串的初始值设定项列表时,我将 2 个迭代器传递给一组字符串。
即便如此,为什么编译器会将此标记为不明确的调用?
编译器发现对以下两个构造函数的调用不明确(注意它们都没有初始化列表):
template <class InputIt>
std::vector::vector (InputIt first, InputIt last, const Allocator& alloc = Allocator());
和
template <class InputIt>
std::string::string (InputIt first, InputIt last, const Allocator& alloc = Allocator());
现在,如果您要使用这些迭代器参数实际调用std::string
构造函数,则会出现错误,因为它们没有取消对字符的引用。 但是由于该检查不是函数声明的一部分(例如,通过 SFINAE),因此您会收到歧义错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.