gpt4 book ai didi

c++ - Lambda 上的运算符 +

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:02:13 24 4
gpt4 key购买 nike

谁能帮我指出为什么这在 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))'

这是强制转换 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.

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

最佳答案

显然这是一个 VC++ 错误。似乎没有其他方法只能显式转换:

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

不幸的是,魔法就这样失去了。

关于c++ - Lambda 上的运算符 +,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26220448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com