Mistake on this page?
Report an issue in GitHub or email us
NonCopyable< T > Class Template Reference

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

#include <NonCopyable.h>

Detailed Description

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

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


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 {
virtual ~Connection();
virtual void open() = 0;
class SerialConnection : public Connection {
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 {
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.
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 168 of file NonCopyable.h.

Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.