如何在一般的lambda中完美地转发`auto &&`?

时间:2022-12-03 18:59:21

C++14 supports generic lambdas. However, the following code is rejected by clang 3.4.

C ++ 14支持通用lambdas。但是,clang 3.4拒绝以下代码。

#include <utility>

void f(int);
void f(int&);

int main()
{
    [](auto&& v) { f(std::forward<auto>(v)); }(8); // error
}

How to perfectly forward auto&& in a generic lambda?

如何在一般的lambda中完美地转发auto &&?

1 个解决方案

#1


26  

auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype?

汽车不是一种类型所以我并不感到惊讶,这不起作用。但是你不能使用decltype吗?

[](auto&& v) { f(std::forward<decltype(v)>(v)); }(8);

Scott Meyers has more details.

Scott Meyers有更多细节。

#1


26  

auto is not a type so I’m not surprised this doesn’t work. But can’t you use decltype?

汽车不是一种类型所以我并不感到惊讶,这不起作用。但是你不能使用decltype吗?

[](auto&& v) { f(std::forward<decltype(v)>(v)); }(8);

Scott Meyers has more details.

Scott Meyers有更多细节。