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

#include <NonCopyable.h>

Protected Member Functions

 NonCopyable ()
 
 ~NonCopyable ()
 
 NonCopyable (const NonCopyable &)
 
NonCopyableoperator= (const NonCopyable &)
 

Detailed Description

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

Inheriting from this class autogeneration of copy construction and copy assignment operations.

Classes which are not value type should inherit privately from this class to avoid generation of invalid copy constructor or copy assignment operator which can lead to unnoticeable programming errors.

As an example consider the following signature:

class Resource;
class Foo {
public:
Foo() : _resource(new Resource()) { }
~Foo() { delete _resource; }
private:
Resource* _resource;
}
Foo get_foo();
Foo foo = get_foo();

There is a bug in this function, it returns a temporary value which will be byte copied into foo then destroyed. Unfortunately, internally the Foo class manage a pointer to a Resource object. This pointer will be released when the temporary is destroyed and foo will manage a pointer to an already released Resource.

Two issues has to be fixed in the example above:

// return a reference to an already managed Foo instance
Foo& get_foo();
Foo& foo = get_foo();
// create a new Foo instance
Foo* make_foo();
Foo* m = make_foo();
class Foo {
public:
Foo() : _resource(new Resource()) { }
~Foo() { delete _resource; }
private:
// disallow copy operations
Foo(const Foo&);
Foo& operator=(const Foo&);
// data members
Resource* _resource;
}

Another solution is to inherit privately from the NonCopyable class. It reduces the boiler plate needed to avoid copy operations but more importantly it clarifies the programmer intent and the object semantic.

class Foo : private NonCopyable<Foo> { public: Foo() : _resource(new Resource()) { } ~Foo() { delete _resource; } private: Resource* _resource; }

Template Parameters
TThe type that should be made non copyable. It prevent cases where the empty base optimization cannot be applied and therefore ensure that the cost of this semantic sugar is null.

As an example, the empty base optimization is prohibited if one of the empty base class is also a base type of the first non static 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 shall 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 makes 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).
Note
Compile time errors are disabled if the develop or the release profile is used. To override this behavior and force compile time errors in all profile set the configuration parameter "platform.force-non-copyable-error" to true.

Constructor & Destructor Documentation

template<typename T>
mbed::NonCopyable< T >::NonCopyable ( )
inlineprotected

Disallow construction of NonCopyable objects from outside of its hierarchy.

template<typename T>
mbed::NonCopyable< T >::~NonCopyable ( )
inlineprotected

Disallow destruction of NonCopyable objects from outside of its hierarchy.

template<typename T>
mbed::NonCopyable< T >::NonCopyable ( const NonCopyable< T > &  )
inlineprotected

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 non copyable happens.

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

Member Function Documentation

template<typename T>
NonCopyable& mbed::NonCopyable< T >::operator= ( const NonCopyable< T > &  )
inlineprotected

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 non copyable happens.

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


The documentation for this class was generated from the following file:
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.