Boost logo

Boost :

Subject: Re: [boost] [core] [noncopyable] Add nonmoveable?
From: Gavin Lambert (gavinl_at_[hidden])
Date: 2017-04-26 00:00:48


On 25/04/2017 22:23, Andrey Semashev via Boost wrote:
> On 04/25/17 13:10, Viktor Sehr via Boost wrote:
>> In addition to noncopyable, I sometimes want to force classes to be
>> nonmoveable as well (mainly node-type classes in tree structures which
>> other classes point to). Therefore I'd suggest adding a cousin to
>> noncopyable; boost::nonmoveable which simply prevents an instance to be
>> nonmoveable (as well as noncopyable).
>>
>> Note; even due the delete modifier were added in C++11 I still think
>> inheriting boost:noncopyable\nonmoveable syntactically nicer than
>> manually
>> marking the copy\move constructors\assignment operators delete.
>
> I think in C++11 noncopyable should be considered deprecated and
> generally avoided. It affects class hierarchy, adds an extra namespace
> to ADL and may not be optimized away with EBO. I would even avoid it in
> C++03 as well. It follows that nonmoveable makes no sense in C++11. Just
> use the language features you have.

boost::noncopyable has protection so it won't ADL to the boost:: namespace.

Also, since it does not explicitly declare move constructors either way,
anything that inherits from boost::noncopyable is also non-moveable by
default, which is usually what you want.

The C++11 way, however, would be to use a std::unique_ptr as a private
member of your class (eg. for PIMPL), which automatically makes your
class moveable but not copyable via Rule of Zero.

Or use shared_ptr if you want your class to be copyable but have all
copies point to a shared implementation (handle-body or reference idiom).


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk