Professional CMake:

A Practical Guide

Go beyond trivial examples and learn how to use CMake effectively with practical advice direct from one of the CMake co-maintainers

Slider

Iterating over an enum

The enum feature of C/C++ is far from new. It is very useful for defining a specific set of values that a simple integer may take, which can lead to clearer, more concise code when used appropriately. Many compilers are capable of warning about common errors associated with enum use, such as not including case statements for all possible enum values in a switch statement that has no default clause. In many respects, an enum acts like a set, but being essentially just a glorified int, it lacks any of the container features of something like std::set.

The developer typically faces a tradeoff between performance and functionality when deciding between an enum or some kind of set-like container. There are some (often common) situations, however, where an enum can still be treated like a container, thanks to features made available in C++11.

Read moreIterating over an enum

Container iteration with C++11

C++11 introduced some features which make working with STL containers much easier. One common situation is the need to iterate over a container and to perform some operation(s) on each item. Consider the following typical example:

std::vector<SomeType> container;
// ...

for(std::vector<SomeType>::iterator iter = container.begin();
    iter != container.end();
    ++iter)
{
    const SomeType& item = *iter;
    // ...
}

This syntax has a couple of drawbacks: