[move] Declarations of the copy constructor and copy-assign operator produced by BOOST_MOVABLE_BUT_NOT_COPYABLE

Looking through the sources of Boost.Move in trunk, I am wondering about a couple of lines in <boost/move/move.hpp> that are part of the expansion of BOOST_MOVABLE_BUT_NOT_COPYABLE when BOOST_NO_RVALUE_REFERENCES is defined: #define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\ private:\ TYPE(TYPE &);\ TYPE& operator=(TYPE &);\ public:\ operator ::boost::rv<TYPE>&() \ { return *static_cast< ::boost::rv<TYPE>* >(this); }\ operator const ::boost::rv<TYPE>&() const \ { return *static_cast<const ::boost::rv<TYPE>* >(this); }\ private:\ // Why do the declarations of the copy constructor and copy-assign operator overload not take a const-reference to TYPE? When BOOST_NO_RVALUE_REFERENCES is not defined, they do: #define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\ public:\ typedef int boost_move_emulation_t;\ private:\ TYPE(const TYPE &);\ TYPE& operator=(const TYPE &);\ // Joseph Trebbien

On Thu, Apr 14, 2011 at 9:23 AM, Joseph Trebbien <jtrebbien@gmail.com>wrote:
Looking through the sources of Boost.Move in trunk, I am wondering about a couple of lines in <boost/move/move.hpp> that are part of the expansion of BOOST_MOVABLE_BUT_NOT_COPYABLE when BOOST_NO_RVALUE_REFERENCES is defined:
#define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\ private:\ TYPE(TYPE &);\ TYPE& operator=(TYPE &);\ public:\ operator ::boost::rv<TYPE>&() \ { return *static_cast< ::boost::rv<TYPE>* >(this); }\ operator const ::boost::rv<TYPE>&() const \ { return *static_cast<const ::boost::rv<TYPE>* >(this); }\ private:\ //
Why do the declarations of the copy constructor and copy-assign operator overload not take a const-reference to TYPE? When BOOST_NO_RVALUE_REFERENCES is not defined, they do:
#define BOOST_MOVABLE_BUT_NOT_COPYABLE(TYPE)\ public:\ typedef int boost_move_emulation_t;\ private:\ TYPE(const TYPE &);\ TYPE& operator=(const TYPE &);\ //
In C++03 (when BOOST_NO_RVALUE_REFERENCES is defined), to prevent TYPE-rvalues from binding to the copy constructor and copy assignment operator, we need to declare the copy constructor's and copy assignment operator's argument as reference-to-non-const (TYPE&). This forces TYPE-rvalues to bind to the user-provided move constructor and move assignment operators (whose arguments are boost::rv< TYPE >&) via the implicit conversion to boost::rv< TYPE >&. - Jeff
participants (2)
-
Jeffrey Lee Hellrung, Jr.
-
Joseph Trebbien