Added BOOST_MOVE_BASE utility

This commit is contained in:
Ion Gaztañaga
2014-11-01 19:32:36 +01:00
parent 8355309c5f
commit ad50fba54f
3 changed files with 40 additions and 13 deletions

View File

@ -532,19 +532,26 @@ care to achieve portable and efficient code when using the library with C++03 co
[section:emulation_limitations_base Initializing base classes]
When initializing base classes in move constructors, users must
cast the reference to a base class reference before moving it. Example:
cast the reference to a base class reference before moving it or just
use `BOOST_MOVE_BASE`. Example:
[c++]
//Portable and efficient
Derived(BOOST_RV_REF(Derived) x) // Move ctor
: Base(boost::move(static_cast<Base&>(x))),
mem_(boost::move(x.mem_)) { }
: Base(boost::move(static_cast<Base&>(x)))
//...
or
[c++]
Derived(BOOST_RV_REF(Derived) x) // Move ctor
: Base(BOOST_MOVE_BASE(Base, x))
//...
If casting is not performed the emulation will not move construct
the base class, because no conversion is available from `BOOST_RV_REF(Derived)`
to `BOOST_RV_REF(Base)`. Without the cast we might obtain a compilation
the base class, because no conversion is available from `BOOST_RV_REF(Derived)` to
`BOOST_RV_REF(Base)`. Without the cast or `BOOST_MOVE_BASE` we might obtain a compilation
error (for non-copyable types) or a less-efficient move constructor (for copyable types):
[c++]
@ -552,8 +559,8 @@ error (for non-copyable types) or a less-efficient move constructor (for copyabl
//If Derived is copyable, then Base is copy-constructed.
//If not, a compilation error is issued
Derived(BOOST_RV_REF(Derived) x) // Move ctor
: Base(boost::move(x)),
mem_(boost::move(x.mem_)) { }
: Base(boost::move(x))
//...
[endsect]
@ -755,6 +762,12 @@ Many thanks to all boosters that have tested, reviewed and improved the library.
[section:release_notes Release Notes]
[section:release_notes_boost_1_58_00 Boost 1.58 Release]
* Added `BOOST_MOVE_BASE` utility.
[endsect]
[section:release_notes_boost_1_57_00 Boost 1.57 Release]
* Added `unique_ptr` smart pointer. Thanks to Howard Hinnant for his excellent unique_ptr emulation code and testsuite.

View File

@ -64,19 +64,19 @@ class Derived : public Base
// Compiler-generated copy constructor...
Derived(BOOST_RV_REF(Derived) x) // Move ctor
: Base(boost::move(static_cast<Base&>(x))),
: Base(BOOST_MOVE_BASE(Base, x)),
mem_(boost::move(x.mem_)) { }
Derived& operator=(BOOST_RV_REF(Derived) x) // Move assign
{
Base::operator=(boost::move(static_cast<Base&>(x)));
mem_ = boost::move(x.mem_);
Base::operator=(BOOST_MOVE_BASE(Base, x));
mem_ = boost::move(x.mem_);
return *this;
}
Derived& operator=(BOOST_COPY_ASSIGN_REF(Derived) x) // Copy assign
{
Base::operator=(static_cast<const Base&>(x));
Base::operator=(x);
mem_ = x.mem_;
return *this;
}

View File

@ -198,6 +198,10 @@
boost::move_detail::move_return< RET_TYPE >(REF)
//
#define BOOST_MOVE_BASE(BASE_TYPE, ARG) \
::boost::move((BASE_TYPE&)(ARG))
//
//////////////////////////////////////////////////////////////////////////////
//
// BOOST_MOVABLE_BUT_NOT_COPYABLE
@ -373,7 +377,6 @@
const TYPE & \
//
#endif //#if !defined(BOOST_MOVE_DOXYGEN_INVOKED)
#if !defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
@ -436,6 +439,17 @@
#endif //!defined(BOOST_MOVE_MSVC_AUTO_MOVE_RETURN_BUG) || defined(BOOST_MOVE_DOXYGEN_INVOKED)
//!This macro is used to achieve portable optimal move constructors.
//!
//!When implementing the move constructor, in C++03 compilers the moved-from argument must be
//!cast to the base type before calling `::boost::move()` due to rvalue reference limitations.
//!
//!In C++11 compilers the cast from a rvalue reference of a derived type to a rvalue reference of
//!a base type is implicit.
#define BOOST_MOVE_BASE(BASE_TYPE, ARG) \
::boost::move((BASE_TYPE&)(ARG))
//
namespace boost {
namespace move_detail {