Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c++11

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::function<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?

like image 956
Eelke Avatar asked Aug 21 '11 18:08

Eelke


1 Answers

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.

like image 118
Puppy Avatar answered Sep 27 '22 15:09

Puppy