From 1057ff4d9e8a9b01dc237a6b870bab532e01abfb Mon Sep 17 00:00:00 2001 From: "Vicente J. Botet Escriba" Date: Wed, 10 Apr 2013 17:16:02 +0000 Subject: [PATCH] Utility/noncopyable: Make use of =delete #6578. [SVN r83833] --- include/boost/noncopyable.hpp | 18 +++++++++++++++--- utility.htm | 10 +++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/boost/noncopyable.hpp b/include/boost/noncopyable.hpp index 7770bdb..eb8e2e7 100644 --- a/include/boost/noncopyable.hpp +++ b/include/boost/noncopyable.hpp @@ -9,6 +9,8 @@ #ifndef BOOST_NONCOPYABLE_HPP_INCLUDED #define BOOST_NONCOPYABLE_HPP_INCLUDED +#include + namespace boost { // Private copy constructor and copy assignment ensure classes derived from @@ -21,11 +23,21 @@ namespace noncopyable_ // protection from unintended ADL class noncopyable { protected: - noncopyable() {} +#ifndef BOOST_NO_DEFAULTED_FUNCTIONS + BOOST_CONSTEXPR noncopyable() = default; + ~noncopyable() = default; +#else + noncopyable() {} ~noncopyable() {} - private: // emphasize the following members are private +#endif +#ifndef BOOST_NO_DELETED_FUNCTIONS + noncopyable( const noncopyable& ) = delete; + noncopyable& operator=( const noncopyable& ) = delete; +#else + private: // emphasize the following members are private noncopyable( const noncopyable& ); - const noncopyable& operator=( const noncopyable& ); + noncopyable& operator=( const noncopyable& ); +#endif }; } diff --git a/utility.htm b/utility.htm index 94920c3..974776e 100644 --- a/utility.htm +++ b/utility.htm @@ -84,8 +84,10 @@ const std::list<T>::iterator next = boost::next(prev, 2); will prevent the otherwise implicitly-generated functions (which don't have the proper semantics) from becoming a trap for other programmers.

The traditional way to deal with these is to declare a private copy constructor - and copy assignment, and then document why this is done.  But deriving - from noncopyable is simpler and clearer, and doesn't require additional + and copy assignment, and then document why this is done.  A new alternative + was introduced in C++2011, declaring a copy constructor and a copy assignment + operator, but marking both as deleted.  Deriving + from noncopyable is simpler and clearer, and doesn't require additional documentation.

The program noncopyable_test.cpp can be used to verify class noncopyable works as expected. It has have been run @@ -106,7 +108,9 @@ class ResourceLadenFileSystem : boost::noncopyable { about the effect on compiler optimization of adding (even trivial inline) destructor declarations. He says "Probably this concern is misplaced, because noncopyable will be used mostly for classes which own resources and - thus have non-trivial destruction semantics."

+ thus have non-trivial destruction semantics."  With C++2011, using an + optimized and trivial constructor and similar destructor can be enforced by + declaring both and marking them default.

Function template addressof()

Function addressof() returns the address of an object.