The official mbed C/C++ SDK provides the software platform and libraries to build your applications.

Dependents:   hello SerialTestv11 SerialTestv12 Sierpinski ... more

Embed: (wiki syntax)

« Back to documentation index

NonCopyable< T > Class Template Reference

NonCopyable< T > Class Template Reference
[NonCopyable class]

Prevents generation of copy constructor and copy assignment operator in derived classes. More...

#include <NonCopyable.h>

Protected Member Functions

 NonCopyable ()
 Disallow construction of NonCopyable objects from outside of its hierarchy.
 ~NonCopyable ()
 Disallow destruction of NonCopyable objects from outside of its hierarchy.
 MBED_DEPRECATED ("Invalid copy construction of a NonCopyable resource.") NonCopyable(const NonCopyable &)
 NonCopyable copy constructor.
 MBED_DEPRECATED ("Invalid copy assignment of a NonCopyable resource.") NonCopyable &operator
 NonCopyable copy assignment operator.
NonCopyableoperator= (const NonCopyable &)
 Declare copy assignment operator as private.

Detailed Description

template<typename T>
class mbed::NonCopyable< T >

Prevents generation of copy constructor and copy assignment operator in derived classes.

Usage

To prevent generation of copy constructor and copy assignment operator, inherit privately from the NonCopyable class.

 class Resource : NonCopyable<Resource> { };
 
 Resource r;
 // generates compile time error:
 Resource r2 = r;
Background information

Instances of polymorphic classes are not meant to be copied. The C++ standards generate a default copy constructor and copy assignment function if these functions have not been defined in the class.

Consider the following example:

 // base class representing a connection
 struct Connection { 
     Connection();
     virtual ~Connection();
     virtual void open() = 0;
 }
 
 class SerialConnection : public Connection { 
 public:
     SerialConnection(Serial*);
 
 private:
     Serial* _serial;
 };
 
 Connection& get_connection() { 
     static SerialConnection serial_connection;
     return serial_connection;
 }

 Connection connection = get_connection();

There is a subtle bug in this code, the function get_connection returns a reference to a Connection which is captured by value instead of reference.

When `get_connection` returns a reference to serial_connection it is copied into the local variable connection. The vtable and others members defined in Connection are copied, but members defined in SerialConnection are left apart. This can cause severe crashes or bugs if the virtual functions captured use members not present in the base declaration.

To solve that problem, the copy constructor and assignment operator have to be declared (but don't need to be defined) in the private section of the Connection class:

 struct Connection { 
 private:
     Connection(const Connection&);
     Connection& operator=(const Connection&);
 }

Although manually declaring private copy constructor and assignment functions works, it is not ideal. These declarations are usually easy to forget, not immediately visible, and may be obscure to uninformed programmers.

Using the NonCopyable class reduces the boilerplate required and expresses the intent because class inheritance appears right after the class name declaration.

 struct Connection : private NonCopyable<Connection> { 
      // regular declarations
 }
Implementation details

Using a template type prevents cases where the empty base optimization cannot be applied and therefore ensures that the cost of the NonCopyable semantic sugar is null.

As an example, the empty base optimization is prohibited if one of the empty base classes is also a base type of the first nonstatic data member:

 struct A { };
 struct B : A {
    int foo;
 };
 // thanks to empty base optimization, sizeof(B) == sizeof(int)

 struct C : A {
   B b;
 };

 // empty base optimization cannot be applied here because A from C and A from
 // B have a different address. In that case, with the alignment
 // sizeof(C) == 2* sizeof(int)

The solution to that problem is to templatize the empty class to make it unique to the type it is applied to:

 template<typename T>
 struct A<T> { };
 struct B : A<B> {
    int foo;
 };
 struct C : A<C> {
   B b;
 };

 // empty base optimization can be applied B and C does not refer to the same
 // kind of A. sizeof(C) == sizeof(B) == sizeof(int).
Template Parameters:
TThe type that should be made noncopyable.
Note:
Compile time errors are disabled if you use the develop or release profile. To override this behavior and force compile time errors in all profiles, set the configuration parameter "platform.force-non-copyable-error" to true.

Definition at line 167 of file NonCopyable.h.


Constructor & Destructor Documentation

NonCopyable (  ) [protected]

Disallow construction of NonCopyable objects from outside of its hierarchy.

Definition at line 173 of file NonCopyable.h.

~NonCopyable (  ) [protected]

Disallow destruction of NonCopyable objects from outside of its hierarchy.

Definition at line 177 of file NonCopyable.h.


Member Function Documentation

MBED_DEPRECATED ( "Invalid copy construction of a NonCopyable< T > resource."   ) const [protected]

NonCopyable copy constructor.

A compile time warning is issued when this function is used, and a runtime warning is printed when the copy construction of the noncopyable happens.

If you see this warning, your code is probably doing something unspecified. Copying of noncopyable resources can lead to resource leak and random error.

Definition at line 189 of file NonCopyable.h.

MBED_DEPRECATED ( "Invalid copy assignment of a NonCopyable< T > resource."   ) [protected]

NonCopyable copy assignment operator.

A compile time warning is issued when this function is used, and a runtime warning is printed when the copy construction of the noncopyable happens.

If you see this warning, your code is probably doing something unspecified. Copying of noncopyable resources can lead to resource leak and random error.

NonCopyable& operator= ( const NonCopyable< T > &   ) [protected]

Declare copy assignment operator as private.

Any attempt to copy assign a NonCopyable will fail at compile time.