在lambda中完美转发? [英] Perfect forwarding in a lambda?

查看:252
本文介绍了在lambda中完美转发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有一项功能,可以编写:

With a function, one can write:

template <class T> void f(T&& x) {myfunction(std::forward<T>(x));}

但是带有lambda,我们没有T:

but with a lambda, we don't have T:

auto f = [](auto&& x){myfunction(std::forward</*?*/>(x));}

如何在lambda中进行完美转发? decltype(x)是否可以用作std::forward中的类型?

How to do perfect-forwarding in a lambda? Does decltype(x) work as the type in std::forward?

推荐答案

转发绑定到转发引用的lambda参数的规范方法确实是使用decltype:

The canonical way to forward a lambda argument that was bound to a forwarding reference is indeed with decltype:

auto f = [](auto&& x){
  myfunction(std::forward<decltype(x)>(x));
} //                      ^^^^^^^^^^^

这篇关于在lambda中完美转发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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