Designofcopyableandmovabletypes
|
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 > | |
T | boost::copy_adl (T const &x) |
template<class T > | |
T | boost::copy (T const &x) |
Makes an explicit copy of x . More... | |
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:
BOOST_MOVABLE2(TYPE)
, and doesn't work when your class uses BOOST_COPYABLE_AND_MOVABLE()
.When not to use BOOST_MOVABLE2(TYPE)
:
noexcept
swap; or#define BOOST_MOVABLE2 | ( | TYPE | ) |
Use this macro to make your class movable.
When using this macro in a class:
noexcept
swap()
for your class;BOOST_DELETED_COPY_CONSTRUCTOR(TYPE)
if you want a movable-only class;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;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: 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
;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.
#define BOOST_DELETED_COPY_CONSTRUCTOR | ( | TYPE | ) |
This macro disables the copy constructor in a way compatible with BOOST_MOVABLE2(TYPE)
for your compiler.
#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.
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.
#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:
move()
(moving will happen automatically, and wrapping with move()
may disable some move-elision opportunities;move()
is necessary. 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().
T boost::copy | ( | T const & | x | ) |
Makes an explicit copy of x
.
This function simply makes an unqualified call:
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().