运算符+ on Lambda [英] operator + on Lambda

查看:142
本文介绍了运算符+ on Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我指出为什么这在VS2013中不起作用。

  auto p = + [] > void {std :: cout< Hello,world!\\\
; };
p();

source_file.cpp(7):错误C2593:'operator +'是不明确的
可以是内置的C ++操作符+(void(__cdecl *)(void))'
或'内置的C ++操作符+(void(__fastcall *)(void))'
或'内置的C ++操作符(void(__stdcall *) C ++操作符+(void(__vectorcall *)(void))'

转换lambda


5.1.2 Lambda表达式[expr.prim.lambda]



6 没有 lambda-capture 表达式的闭包类型具有公共非虚拟非显式const转换函数,指向具有与闭包类型的函数调用操作符相同的参数和返回类型的函数的指针。这个转换函数返回的值应该是一个函数的地址,当被调用时,函数调用闭包类型的函数调用操作符的效果相同。


我如何告诉编译器他应该使用什么转换?

解决方案显然这是一个VC ++错误。看起来没有别的方法可以显式地转换:

  + static_cast< void(*)()>([] {}); //空白兰布不失一般性

不幸的是,魔法会像这样丢失。


Can anyone help me pointing out why this does not work in VS2013?

auto p = +[]() -> void { std::cout << "Hello, world!\n"; };
p();

source_file.cpp(7) : error C2593: 'operator +' is ambiguous
    could be 'built-in C++ operator+(void (__cdecl *)(void))'
    or       'built-in C++ operator+(void (__stdcall *)(void))'
    or       'built-in C++ operator+(void (__fastcall *)(void))'
    or       'built-in C++ operator+(void (__vectorcall *)(void))'

This is a legal operator to force-cast the lambda

5.1.2 Lambda expressions [expr.prim.lambda]

6 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.

How can I tell the compiler what conversion he should use?

解决方案

Apparently this is a VC++ bug. There seems to be no other way than to explicitly cast:

+ static_cast< void(*)() >( []{} ); // Empty lamba without loss of generality

Unfortunately, the magic is lost like this.

这篇关于运算符+ on Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆