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

不羁岁月 提交于 2021-02-15 10:16:25

问题


C++14 supports generic lambdas. However, the following code is rejected by 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?


回答1:


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

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

Scott Meyers has more details.



来源:https://stackoverflow.com/questions/24535430/how-to-perfectly-forward-auto-in-a-generic-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!