Boost logo

Boost :

Subject: [boost] [config] New NO_COPYABLE, DELETE_COPY_CTOR and DELETE_COPY_ASSIGN macros
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-05-01 03:50:45


Hi,

I'm using the following BOOST_NO_COPYABLE, BOOST_DELETE_COPY_CTOR,
BOOST_DELETE_COPY_ASSIGN and macros in Boost.Thread (see
http://svn.boost.org/svn/boost/trunk/boost/thread/detail/delete.hpp). I
guess some of you have their own similar macros.

For uniformity purposes, couldn't them be added to Boost.Config/ Boost
Helper Macros <cid:part1.09000208.02050709_at_[hidden]> if I provide a
patch with doc and tests?

Best,
Vicente

[section:delete `=delete` emulation]

C++11 allows to delete some implicitly generated functions as
constructors and assignment using '= delete' as in

   public:
     CLASS(CLASS const&) = delete;
     CLASS& operator=(CLASS const&) = delete;

On compilers not supporting this feature, we can relays on a partial
simulation, by declaring the function as private without definition.

   private:
     CLASS(CLASS &);
     CLASS& operator=(CLASS&);

The emulation is good but partial as the private functions are still
visible.

To have the best the user should write

       public:
#ifndef BOOST_NO_DELETED_FUNCTIONS
       CLASS(CLASS const&) = delete;
       CLASS& operator=(CLASS const&) = delete;
#else // BOOST_NO_DELETED_FUNCTIONS
     private:
       CLASS(CLASS&);
       CLASS& operator=(CLASS&);
     public:
#endif // BOOST_NO_DELETED_FUNCTIONS

The following macros allows to take in account the features the compiler
provides and have the same effect

   public:
     BOOST_DELETE_COPY_CTOR(CLASS)
     BOOST_DELETE_COPY_ASSIGN(CLASS)

As these come together quite often when we want to make a non copyable
class the user can use instead

   public:
     BOOST_NO_COPYABLE(CLASS)

[section:delete_copy_ctor `BOOST_DELETE_COPY_CTOR(CLASS)`]

This macro deletes the copy constructor when the compiler supports it or
makes it private. It can be included on the public section only.

[endsect]

[section:delete_copy_assign `BOOST_DELETE_COPY_ASSIGN(CLASS)`]

This macro deletes the copy assignment when the compiler supports it or
makes it private. It can be included on the public section only.

[endsect]

[section:no_copyable `BOOST_NO_COPYABLE(CLASS)`]

This macro makes a class as no copyable, disabling copy construction and
assignment. It can be included on the public section only.

[endsect]

[endsect]


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