我有一個奇怪的錯誤,我真的不明白,VS2013。 這只是我的真正問題導致相同的錯誤簡化。沒有可執行此轉換的用戶定義轉換運算符,或者不能調用運算符
std::function<bool()> x = (someCondition == true)
? []() { return true; }
: []() { return false; };
VS編譯器錯誤是:
1>f:\test\cppconsoleapplication\cppconsoleapplication.cpp(497): error C2446: ':' : no conversion from 'main::<lambda_96d01fe3721e46e4e8217a69a07d151b>' to 'main::<lambda_0d38919a9b2aba5caf910d83eac11776>'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
智能感知,甚至想出了這個神祕的錯誤消息:
IntelliSense: more than one operator "?" matches these operands:
built-in operator "expression ? pointer : pointer"
built-in operator "expression ? pointer : pointer"
built-in operator "expression ? pointer : pointer"
built-in operator "expression ? pointer : pointer"
operand types are: lambda []bool()->bool : lambda []bool()->bool f:\Test\CppConsoleApplication\CppConsoleApplication.cpp 496
而下面編譯
std::function<bool()> x = []() { return true; };
if (someCondition == false)
x = []() { return false; };
難道僅僅是VisualStudi之一o的錯誤,或者我在這裏做錯了什麼?
VS2013沒有完全支持C++ 11功能。試試VS2015。您也可以嘗試將一個lambda放入函數指針中,以強制扣除常見類型。 –