完美转发对象的成员 [英] Perfect forwarding a member of object
本文介绍了完美转发对象的成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有两个 struct
s:
struct X {};
struct Y { X x; }
我有功能:
void f(X&);
void f(X&&);
如何写一个函数 g()
Y&
或 Y&&&
但完美转寄 X&
或 X&&&
到 f()
:
How do I write a function g()
that takes Y&
or Y&&
but perfect forwarding X&
or X&&
to f()
, respectively:
template <typename T>
void g(T&& t) {
if (is_lvalue_reference<T>::value) {
f(t.x);
} else {
f(move(t.x));
}
}
上述代码说明了我的意图,随着参数数量的增长。
The above code illustrate my intention but is not very scalable as the number of parameters grows. Is there a way make it work for perfect forwarding and make it scalable?
推荐答案
template <typename T>
void g(T&& t) {
f(std::forward<T>(t).x);
}
这篇关于完美转发对象的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文