如何解决采用std :: string和std :: vector的构造函数之间的歧义

时间:2018-12-13 16:32:18

标签: c++ overloading ambiguous-call

我想制作一个“标记”类,其名称可以指定为点分隔的名称(如"this.is.my.name"或字符串向量(如{"this","is","my","name"})。

当我尝试执行此操作时,编译器会有时告诉我我的调用不明确。我想知道(1)为什么这根本是模棱两可的,以及(2)为什么有时它只是模棱两可的。

这是我的示例代码,您也可以查看和编译here on Coliru

#include <string>
#include <vector>
#include <iostream>

class Tag
{
public:
    explicit Tag(std::string name);
    explicit Tag(std::vector<std::string> name);

};

Tag::Tag(std::string name)
{
    //here 'name' will be a dotted collection of strings, like "a.b.c"
}

Tag::Tag(std::vector<std::string> name)
{
    //here 'name' will be a vector of strings, like {"a","b","c"}
}


int main(int argc, char**argv)
{
    Tag imaTag{{"dotted","string","again"}};
    Tag imaTagToo{"dotted.string"};

    //everything is fine without this line:
    Tag imaTagAlso{{"dotted","string"}};
    std::cout << "I made two tags" << std::endl;

}

在指示的行中,出现以下错误:

g++ -std=c++11 -O2 -Wall -pthread main.cpp && ./a.out
main.cpp: In function 'int main(int, char**)':
main.cpp:28:39: error: call of overloaded 'Tag(<brace-enclosed initializer list>)' is ambiguous
     Tag imaTagAlso{{"dotted","string"}};
                                   ^
main.cpp:18:1: note: candidate:     'Tag::Tag(std::vector<std::__cxx11::basic_string<char> >)'
 Tag::Tag(std::vector<std::string> name)
 ^~~
main.cpp:13:1: note: candidate: 'Tag::Tag(std::__cxx11::string)'
 Tag::Tag(std::string name)
 ^~~

1 个答案:

答案 0 :(得分:8)

Tag imaTagAlso{{"dotted","string"}};说构造一个Tag,将其命名为imaTagAlso并用{"dotted","string"}对其进行初始化。与此有关的问题是std::string可以由一对迭代器构造,并且由于字符串文字可能会衰减为const char*,因此它们有资格作为迭代器。因此,您可以使用“迭代器”调用字符串构造函数,也可以使用其std::initializer_list构造函数调用向量构造函数。要解决此问题,您可以使用

Tag imaTagAlso{{{"dotted"},{"string"}}};

表示构造一个Tag,将其命名为imaTagAlso并用{{"dotted"},{"string"}}进行初始化,现在{"dotted"}{"string"}成为{{1}的元素}的向量构造函数。

您也可以(自c ++ 14起)像使用std::string's user defined literal operator (""s)

std::initializer_list

构成 braced-init-list Tag imaTagAlso{{"dotted"s,"string"s}}; 的每个元素,然后选择向量构造函数。