Unique Pointer – std::unique_ptr

With C++11 came a new smart pointer class, std::unique_ptr.

This is a great class to use when you need to allocate an object which must have a life time longer than the current local scope. In old C++, this would be done by creating the object on the free store using new. It would then be up to you as a programmer to decide when the object is no longer necessary and then call delete on the pointer. The new unique pointer frees you from remembering when to call delete. The key feature of the unique pointer is that is just that, unique. There can be one and only one pointer to the managed object and no copies can ever be made. If you need a pointer which can be shared between many classes then you should instead go for shared_ptr.

The unique pointer is stored on the local scope and when it goes out of scope then the contents of the pointer will be deleted.

Non copyable

The key feature with the unique pointer is that it is unique, this means that no copies of the unique_ptr can ever be made. This has as a side effect that an object which owns a unique_ptr as a member variable can itself not be copied. If you think about it, then this is actually the same behavior as in old C++ when you instead would create a pointer using new and store the pointer as a member variable in a class. That pointer caused problems when you tried to make copies of the owning object, because who should own (and then also delete) the pointer after the copy was made? The original object or the copy? In practice this made the object not copyable unless you explicitly wrote an copy constructor and copy assignment operator. The difference is that in the old way would the program still compile and run. Modern C++ stops you from doing this, it tries to stop you from shooting yourself in the foot!

 Movable

The unique pointer is on the other hand movable, this means that it is possible to create a unique_ptr inside a function and then return the created pointer. It also means that objects owning a unique pointer will themselves also be movable without you having to do anything.

Make

C++11 introduced the std::make_shared function which made it possible to create a shared_ptr safely and easily. However, the make_unique function wasn’t added until C++14 simply because of an oversight in the review. If your compiler supports C++14 then you can create the unique pointer like this

where the parameters to the constructor of the class are passed in the parenthesis

Facebookgoogle_pluslinkedinmail

The auto keyword

The auto keyword was changed in C++11 from being completely unnecessary to being a highly useful keyword.

Auto variable type (C++11)

The auto keyword tells the compiler to set the type of a variable to the type of the value that we assign to the variable. For example:

If you don’t assign a value to the variable, then the compiler will be lost and will refuse to compile since it cannot figure out the type to set;

auto can be really useful when some of the standard library operations where the type names can be really complex:

when creating a local lambda expression, then auto is a must:

Auto return type (C++14)

Beginning in C++14 can the return type of a function be auto:

Auto as lambda parameter (C++14)

From C++14 we can also use the auto keyword for the type of the parameters when writing a lambda expression. The lambda itself will then be a generic lambda, i.e. it will be compiled as a template:

will be compiled as:

 

Facebookgoogle_pluslinkedinmail