Designofcopyableandmovabletypes
move

Extension of Boost.Move. More...

Macros

#define BOOST_MOVABLE2(TYPE)
 Use this macro to make your class movable. More...
 
#define BOOST_DELETED_COPY_CONSTRUCTOR(TYPE)
 This macro disables the copy constructor in a way compatible with BOOST_MOVABLE2(TYPE) for your compiler. More...
 
#define BOOST_EXPLICIT_COPY_CONSTRUCTOR(TYPE, x)
 This macro declares an explicit copy constructor in a way compatible with BOOST_MOVABLE2(TYPE) for your compiler; you need to provide its' implementation. More...
 
#define BOOST_MOVE_LOCAL_RETVAL(x)
 This macro should be used whenever you return a local variable. More...
 
template<class T >
boost::copy_adl (T const &x)
 
template<class T >
boost::copy (T const &x)
 Makes an explicit copy of x. More...
 

Detailed Description

Extension of Boost.Move.

If you intend to add move emulation to your type with the use of the pass-by-value and swap idiom in implementing the operator=, which is recommended by here, then you need the macros BOOST_MOVABLE2() and optionally BOOST_DELETED_COPY_CONSTRUCTOR() or BOOST_EXPLICIT_COPY_CONSTRUCTOR(). Otherwise if you want to avoid the pass-by-value and swap idiom, use macros BOOST_COPYABLE_AND_MOVABLE() or MOVABLE_BUT_NOT_COYABLE() from Boost.Move.

You should also consider making your class explicitly copyable, if it's movable. To make explicit copies of objects, use boost::copy() similarily to how you'd use boost::move() to indicate moving a non-temporary is OK. For more, see Design of copyable and movable types.

I recommend BOOST_MOVABLE2(TYPE) by default, because:

When not to use BOOST_MOVABLE2(TYPE):

Macro Definition Documentation

#define BOOST_MOVABLE2 (   TYPE)

Use this macro to make your class movable.

When using this macro in a class:

  • provide a non-member noexcept swap() for your class;
  • do not provide a copy-assignment operator, because it's provided for you, implemented in terms of pass-by-value and swap;
  • specify a variant of the copy constructor in one of the following scenarios:
    • using BOOST_DELETED_COPY_CONSTRUCTOR(TYPE) if you want a movable-only class;
    • using BOOST_EXPLICIT_COPY_CONSTRUCTOR(TYPE,x) if you want an explicitly-copyable class; in C++98 your class won't have an explicit copy constructor and you can copy instances using boost::copy(), but classes containing or inheriting from your class won't have auto-generated copy constructors;
    • you might want to provide a copy constructor, that is explicit in C++11, and implicit in C++98 (so that classes containing or inheriting from your class will have auto-generated copy constructors); it is best to show that directly in your code:
      #ifndef BOOST_NO_RVALUE_REFERENCES
      explicit
      #endif
      TYPE( TYPE const& b ) : ... { ... }
      this can lead to some undetected implicit copies of your class in C++98, which won't compile in C++11 and can be fixed trivially using boost::copy(); attention: do not provide an explicit copy constructor in C++98, because it is dangerous and together with move emulation makes your class behave like the deprecated std::auto_ptr;
    • provide an ordinary copy constructor to make your class implicitly copyable and movable.

If your move constructor is fast and noexcept, it is recommended to make the copy constructor explicit (see boost::copy()), otherwise swap probably isn't fast or noexcept either, in which case you shouldn avoid the copy-and-swap idiom, used in the implementation of this macro.

Attention
It is unspecified, which access specifier is left after this macro.
#define BOOST_DELETED_COPY_CONSTRUCTOR (   TYPE)

This macro disables the copy constructor in a way compatible with BOOST_MOVABLE2(TYPE) for your compiler.

Attention
It is unspecified, which access specifier is left after this macro.
#define BOOST_EXPLICIT_COPY_CONSTRUCTOR (   TYPE,
 
)

This macro declares an explicit copy constructor in a way compatible with BOOST_MOVABLE2(TYPE) for your compiler; you need to provide its' implementation.

In C++98 your class won't have an explicit copy constructor and you can copy instances using boost::copy(), but classes containing or inheriting from your class won't have auto-generated copy constructors.

Attention
It is unspecified, which access specifier is left after this macro.
#define BOOST_MOVE_LOCAL_RETVAL (   x)

This macro should be used whenever you return a local variable.

In C++11 it expands to (x), while in C++11 it expands to ::boost::move(x). It's needed, because:

  • in C++11 you shouldn't wrap local returned variables with move() (moving will happen automatically, and wrapping with move() may disable some move-elision opportunities;
  • in C++98 the move() is necessary.

Function Documentation

template<class T >
T boost::copy_adl ( T const &  x)

This function returns a copy of x obtained by explicitly calling the copy constructor, and it is the default implementation of an ADL customization point called from boost::copy().

template<class T >
T boost::copy ( T const &  x)

Makes an explicit copy of x.

This function simply makes an unqualified call:

return copy_adl(x);

so you can provide an overload of copy_adl() with your type to customize behavior of boost::copy(). See boost::copy_adl() for the default behavior.

For example, copy_adl() is overloaded for cloneable_ptr_t in boost::smart_ptr::copy_adl().