开发者

Ambiguous call (conversion from char* to lambda vs std::string)

开发者 https://www.devze.com 2023-03-29 10:50 出处:网络
According to my compiler gcc-4.6 the call to func in the example below is ambigous. void func(const std::string &str) {

According to my compiler gcc-4.6 the call to func in the example below is ambigous.

void func(const std::string &str) {
}

void func(std::f开发者_运维技巧unction<std::string()> f) {
}

void test() {
    func("Hello");
}

Is the compiler correct in saying this? If I remove the first overload this code won't compile as it will fail to instantiate the templates involved.

Is there anyway to resolve this beside either renaming one of the two functions or by explicitly converting to std::string?


It can be resolved by SFINAE in the constructor of std::function. However, it doesn't seem to be required and isn't provided by GCC. So you can't portably depend on it working out.


You could also add a third overload to explicitly capture the string literal case:

void func(char const* cstr) {
    return func(std::string(cstr));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消