c++ - 使用无捕获 lambda 表达式作为条件运算符的第二个和第三个操作数时出现 MSVC 错误

标签 c++ visual-c++ c++11 visual-studio-2013 language-lawyer

下面的代码被 GCC 和 Clang 欣然接受 -std=c++14但会导致 Visual Studio 2013 出现编译错误。

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main() {
    auto increasing = [](int lhs, int rhs){return lhs < rhs;};
    auto decreasing = [](int lhs, int rhs){return lhs > rhs;};
    std::vector<int> v(0, 10);
    bool increase = true;
    std::sort(v.begin(), v.end(), increase ? increasing : decreasing);
    return 0;
}

错误是:

main.cpp(11): error C2446: ':': no conversion from 'main::<lambda_0228ee097b83254cfd8aa5f4015a245b>' to 'main::<lambda_cb3b816d067baa9d4462132ee332363c>' main.cpp(11): note: No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

我想我的问题是哪个编译器兼容,我猜它不是 MSVC,标准中是否有明确处理这种情况的部分?

最佳答案

由于 lambda 捕获都不能将它们转换为具有兼容签名的函数指针,因此 gccclang 在这里是正确的。

有一个 gcc 错误报告很好地总结了这个主题: [c++ lambda] error in assigning lambda expr though "operator?:" while capturing 涵盖了这一点并说:

The compiler behaviour looks correct to me. The difference of the lambda expressions in bar and foo3 compared to the other two is that these are capture-free lambdas and thus have a conversion function to function pointer.

Each lambda expression corresponds to a unique class type, so what we have in foo1 and foo2 can be compared with the following class-example:

struct A{}; struct B{};
void f() { false ? A() : B(); }

This expression has no common type for the conditional operator and is ill-formed.

What we have in bar and foo3 can be compared with the following class-example :

struct A
{
    typedef void (*F)();
    operator F();
};

struct B
{
    typedef void (*F)();
    operator F();
};

void f() { false ? A() : B(); }

This is well-formed, because in the last step of the conditional operator conversion attempts (5.16p5), more general conversions are attempted and these find the common pointer to function.

5.16p5 说:

Otherwise, the result is a prvalue. If the second and third operands do not have the same type, and either has (possibly cv-qualified) class type, overload resolution is used to determine the conversions (if any) to be applied to the operands (13.3.1.2, 13.6). If the overload resolution fails, the program is ill-formed. Otherwise, the conversions thus determined are applied, and the converted operands are used in place of the original operands for the remainder of this section.

如果我们按如下方式更改您的代码:

int x = 20 ;
auto increasing = [&x](int lhs, int rhs){return lhs < rhs;};
auto decreasing = [&x](int lhs, int rhs){return lhs > rhs;};

gccclang 都会产生错误,clang 表示 ( see it live ):

error: incompatible operand types ('(lambda at prog.cc:8:23)' and '(lambda at prog.cc:9:23)')
std::sort(v.begin(), v.end(), increase ? increasing : decreasing);
                                      ^ ~~~~~~~~~~   ~~~~~~~~~~

供引用draft C++11 standard 5.1.2 [expr.prim.lambda] 说:

The closure type for a lambda-expression with no lambda-capture has a public non-virtual non-explicit const conversion function to pointer to function having the same parameter and return types as the closure type’s function call operator. The value returned by this conversion function shall be the address of a function that, when invoked, has the same effect as invoking the closure type’s function call operator.

措辞在 C++14 标准草案中有所修改,但不会改变此属性。

更新

提交了 bug report .

关于c++ - 使用无捕获 lambda 表达式作为条件运算符的第二个和第三个操作数时出现 MSVC 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27989031/

相关文章:

c++ - 通过范围检查和带符号的 size_type 扩展 std::vector

c++ - 将 stringstream 传递到 unicode 项目中的控制台输出

c++ - 静态变量与模板类的冲突声明

python - 'mod_ty': Undeclared Identifier

visual-c++ - 从发布的文件中删除 PDB 引用

c++ - 模板化成员函数的声明和实现

c++ - GCC 4.4 fails to link valid C++11 code, when options are -std=C++0x -O0

c++ - 什么是 move 语义?

c++ - 创建 C++ Redis 模块 - "does not export RedisModule_OnLoad() symbol"

c++ - 将数组从 C++ 构造函数传递给函数