Qt has a long history. The first stable version was released before the first version of C++ was standardized and long before the different C++ compiler vendors started shipping usable implementations of the C++ standard library. Because of this, Qt often followed (and still follows) some design idioms that feel unnatural to the usual C++ developer.

This can have some downsides for the Qt ecosystem. The evolution of the C++ programming language which sped up quite a bit in the last decade often brings improvements which don’t fit well with the Qt philosophy. In this blog, I offer some ways to work with this.

Range-based for loops

C++11 brought us the range-based for loop which allows easy iteration over any iterable sequence, including common containers such as std::vector, std::map, etc.

for (auto name: names) {
    // ...

When you write code like the above, the compiler (C++11) sees the following:

    auto && __range = names;
    for (auto __begin = begin(names), __end = end(names);
              __begin != __end; ++__begin) {
        auto&& name = *__begin;
        // ...

It takes the begin and end iterators of the names collection, and iterates from one to the other.