C ++完美转发/代理封装成员

时间:2017-02-01 19:12:28

标签: c++ c++11

我一直在网上寻找一种方法来对成员对象进行完美有效的重定向(代理)。但我找不到任何有用的资源。 我认为这是一项任务,而不是程序员经常做的事情(有时很糟糕),知道一种好的,高效的,简洁的(极简主义的)方法可能是一种很好的知识。

我希望您的建议知道我对移动操作的理解是否正确以及此代理是否正确,特别是对于复制和移动构造函数/赋值操作符。

要在移动构造函数/赋值运算符中进行完美转发,我的理解是如果我们确定将std::move传递给封装对象,则使用lvalue,否则std::forward 。 这是对的吗?

这是我的代码

#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;
        }
};

根据Jarod42的评论进行编辑

1 个答案:

答案 0 :(得分:0)

std :: forward本身可以完美转发。它就是为了这个目的。