【发布时间】:2016-11-11 02:52:13
【问题描述】:
我有一个奇怪的错误,我不太明白,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 甚至提出了这个神秘的错误信息:
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; };
这只是 VisualStudio 的错误之一还是我在这里做错了什么?
【问题讨论】:
-
VS2013 不完全支持 C++11 功能。试试VS2015。您也可以尝试将一个 lambda 显式转换为函数指针以强制推断常见类型。
标签: c++ lambda conditional-operator std-function