小编koo*_*ana的帖子

为什么初始化列表中的元素数会导致模糊的调用错误?

为什么doSomething编译器会先调用前两个,但是在列表中使用两个元素会导致调用不明确?

#include <vector>
#include <string>

void doSomething(const std::vector<std::string>& data) {}

void doSomething(const std::vector<int>& data) {}

int main(int argc, char *argv[])
{
    doSomething({"hello"}); // OK
    doSomething({"hello", "stack", "overflow"}); // OK
    doSomething({"hello", "stack"}); // C2668 'doSomething': ambiguous call

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ initializer-list ambiguous overload-resolution

59
推荐指数
3
解决办法
2169
查看次数