當試圖編譯此,令人驚訝,它提供了一個錯誤,因爲lambda函數的參數auto
已經解決,std::string
,編譯器在撥打test
時,不知道如何將std::string
轉換爲int
或Widget
。函數的std ::功能和通用lambda表達式重載:的std :: string優於INT
但是,我不知道爲什麼,編譯器已經choosen第二invok
函數,而不是第一個,當第一個會成功的:
#include <string>
#include <functional>
struct Widget {};
bool test(int);
bool test(Widget);
void invok(std::function<bool(int)>); // #1
void invok(std::function<bool(std::string)>); // #2
int main()
{
// error: unresolved overloaded function type
// invok(test);
// still error: no known conversion from std::string to
// int or Widget
invok([](auto&& x) {
return test(std::forward<decltype(x)>(x));
});
}
這個例子已經從a C++ proposal複製。
啊,SFINAE-不友好的通用lambdas。 –
我贊同這個問題。 – Barry