Smart Pointers

The C++11 standard introduced two new smart pointers, unique_ptr and shared_ptr. These new classes are very helpful for managing dynamically allocated memory. The difference between these two smart pointers is that unique_ptr implies exclusive ownership to a block of memory, whereas shared_ptr allows for multiple ownership. Both these smart pointers are defined in the memory … Read more

Lambda Expressions

Lambda expressions allow you to define anonymous functions in your C++ code. A good way to demonstrate lambda expressions in practice is to show how they can be used in conjunction with some of the algorithms in the standard library. For example, here is how we would sort a list of integers in C++03: bool … Read more

Move Semantics

In the previous post on Rvalue References, we introduced lvalues, rvalues and temporary objects. The purpose of that post was to lay the groundwork for a discussion on move semantics, an important feature which was introduced in the C++11 standard. We again consider the example of a Vector class for 3D geometry. The definition file … Read more

Rvalue References

Rvalues and Lvalues The terms lvalue and rvalue do not typically come up often for most programmers. Occasionally you might see a compiler error message about rvalues. Other than that, it is not something you generally have to worry about. If we consider the simple assignment: a = b there is an obvious symmetry; a … Read more

Delegating Constructors

In some situations, it’s useful to provide more than one way to initialise an object. When we choose to provide more than one constructor, we want to avoid having to duplicate code. Consider the following example of a simple 2D vector class, which can be initialised either by passing in two doubles, or a double … Read more

Range-based Loop Syntax

The C++ standard library comes with a host of useful container classes. However, using these classes means becoming familiar with the language of templates. Simply iterating through a collection requires the use of some quite verbose syntax. For example, suppose we have a vector of integers: std::vector<int> intArray; In C++03, the contents of the vector … Read more

Auto Type Deduction

In C++03, auto specifies that a variable has automatic storage duration. This means that the variable is destroyed when it falls out of scope. This contrasts with static variables which persist for the lifetime of the program. Since the default storage duration is auto, the specifier is usually omitted. In C++11, the auto keyword has … Read more

nullptr

In C++03, there is no concept of null built in to the language. For anyone familiar with C# or Java, this is quite surprising. Prior to C++11, the NULL literal was typically used to overcome this omission. However, there are some downsides to using NULL: The NULL literal is implementation-defined so it could, for example, … Read more

The Pimpl Idiom

One of the strengths of OOP is the ability to separate the interface from the implementation. For example, if you are building a cross-platform library, the interface should be common across all platforms and ideally the API should not bring in any platform-specific definitions or header files. This can be awkward when you want to … Read more

Categories C++

Scoped Enumerations

In C++03, enumerations are declared as follows: enum Suit { Hearts, Diamonds, Clubs, Spades }; Suit suit = Hearts; As can be seen, Hearts is in the global namespace. This could give rise to potential name collisions with other enums. Another issue is that it is difficult to find instances of a particular enum via … Read more