mercredi 1 février 2017

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 / constructor my understanding is that we use std::move if we are sure we pass an lvalue to the encapsulated object, and std::forward otherwise. 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(const TOther &other) :
        variant(other)
        {}

        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(visitor, variant);
        }

        template <class Fn>
        inline void applyVisitor(Fn&& visitor) const {
            boost::apply_visitor(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=(const TOther &other) {
            variant = other;
            return *this;
        }

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

Thank you

Aucun commentaire:

Enregistrer un commentaire