这个问题很可能已经被问到了,但是我没有找到答案。
下面的代码使用gcc进行编译,但在运行时因std :: length_error(live)崩溃。
void test(const std::string &value) { std::cout << "string overload: " << value << std::endl; }
//void test(const std::vector<std::string> &) { std::cout << "vector overload" << std::endl; }
int main()
{
test({"one", "two"});
}
Run Code Online (Sandbox Code Playgroud)
从字符串的初始化程序列表创建字符串的功能似乎有争议,例如,不能使上面的代码注释掉的重载成为可能。
但是,即使允许这种构造,为什么还会导致失败呢?
c++ constructor constructor-overloading initializer-list overload-resolution