[英]C++ perfect forwarding/proxy of encapsulated member
I have been looking online for a way to do a perfect and efficient redirection (proxy) to a member object. 我一直在网上寻找一种方法来对成员对象进行完美而有效的重定向(代理)。 But I can't find any useful resource. 但是我找不到任何有用的资源。 I imagine this is a task than programmers are doing often (sometimes badly), and it could be a good knowledge to know a good, efficient and concise (minimalist) way to do it. 我想这是程序员经常要做的任务(有时很糟糕),并且知道一种好的,高效且简洁的(极简)方法可能是一个很好的知识。
I would like your advice to know if my understanding of move operations is right and if this proxy is correct particularly for copy and move constructor / assignment operators. 我希望您的建议知道我对移动操作的理解是否正确,以及此代理是否正确(特别是对于复制和移动构造函数/赋值运算符)。
To do a perfect forwarding in a move constructor / assignment operator my understanding is that we use std::move
if we are sure we pass an lvalue
to the encapsulated object, and std::forward
otherwise. 为了在move构造函数/赋值运算符中进行完美的转发,我的理解是,如果确定可以将lvalue
传递给封装的对象,则使用std::move
,否则使用std::forward
。 Is it correct ? 这是正确的吗 ?
Here is my code 这是我的代码
#include <boost/variant.hpp>
/* ********************************************************************/
// Class
/* ********************************************************************/
template <class... T>
class BoostVariantWrapper {
/* ********************************************************************/
// Private fields
/* ********************************************************************/
boost::variant<T...> variant;
public:
/* ********************************************************************/
// Constructors / Destructors
/* ********************************************************************/
BoostVariantWrapper() {}
BoostVariantWrapper(const BoostVariantWrapper &other) :
BoostVariantWrapper(other.variant)
{}
BoostVariantWrapper(BoostVariantWrapper &other) :
BoostVariantWrapper(other.variant)
{}
BoostVariantWrapper(BoostVariantWrapper &&other) :
BoostVariantWrapper(std::move(other.variant))
{}
template<class TOther>
BoostVariantWrapper(TOther &&other) :
variant(std::forward<TOther>(other))
{}
/* ********************************************************************/
// Public methods
/* ********************************************************************/
template <class U>
U& get() {
return boost::get<U>(variant);
}
template <class Fn>
inline void applyVisitor(Fn&& visitor) {
boost::apply_visitor(std::forward<Fn>(visitor), variant);
}
template <class Fn>
inline void applyVisitor(Fn&& visitor) const {
boost::apply_visitor(std::forward<Fn>(visitor), variant);
}
/* ********************************************************************/
// Operators
/* ********************************************************************/
BoostVariantWrapper& operator=(const BoostVariantWrapper &other) {
return operator=(other.variant);
}
BoostVariantWrapper& operator=(BoostVariantWrapper &other) {
return operator=(other.variant);
}
BoostVariantWrapper& operator=(BoostVariantWrapper &&other) {
return operator=(std::move(other.variant));
}
template<class TOther>
BoostVariantWrapper& operator=(TOther &&other) {
variant = std::forward<TOther>(other);
return *this;
}
bool operator==(const BoostVariantWrapper &other) const {
return variant == other.variant;
}
bool operator<(const BoostVariantWrapper &other) const {
return variant < other.variant;
}
friend std::ostream& operator<<(std::ostream &os, const BoostVariantWrapper &x) {
os << x.variant;
return os;
}
/* ********************************************************************/
// Serialization
/* ********************************************************************/
template<class Archive>
void serialize(Archive &ar, const unsigned int) {
ar & variant;
}
};
Edited based on comment of Jarod42 根据Jarod42的评论进行编辑
std::forward by itself does perfect forwarding. std :: forward本身可以完成完美的转发。 It is meant for this purpose. 就是为了这个目的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.