mirror of
https://github.com/boostorg/utility.git
synced 2025-05-08 18:34:02 +00:00
parent
2eda3f5299
commit
4080fe22e3
@ -9,6 +9,8 @@
|
||||
#ifndef BOOST_NONCOPYABLE_HPP_INCLUDED
|
||||
#define BOOST_NONCOPYABLE_HPP_INCLUDED
|
||||
|
||||
#include <boost/config.hpp>
|
||||
|
||||
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
|
||||
};
|
||||
}
|
||||
|
||||
|
10
utility.htm
10
utility.htm
@ -84,8 +84,10 @@ const std::list<T>::iterator next = boost::next(prev, 2);</pre>
|
||||
will prevent the otherwise implicitly-generated functions (which don't have the
|
||||
proper semantics) from becoming a trap for other programmers.</p>
|
||||
<p>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 <b>noncopyable</b> 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 <code>delete</code>d. Deriving
|
||||
from <b>noncopyable</b> is simpler and clearer, and doesn't require additional
|
||||
documentation.</p>
|
||||
<p>The program <a href="noncopyable_test.cpp">noncopyable_test.cpp</a> can be used
|
||||
to verify class <b>noncopyable</b> 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."</p>
|
||||
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 <code>default</code>.</p>
|
||||
<h2><a name="addressof">Function template addressof()</a></h2>
|
||||
<p>Function <strong>addressof()</strong> returns the address of an object.</p>
|
||||
<blockquote>
|
||||
|
Loading…
x
Reference in New Issue
Block a user