C++11 features

One will often hear that Qt containers haven't changed since the Qt 4 days, but that is not entirely true. Not much work has been done, but there hasn't been a standstill either. First, the move constructors and assignment operators were added in Qt 5.2 for the container classes, as can be seen in the definition of QMap:

QMap(QMap<Key, T> &&other)
QMap<Key, T> & operator=(QMap<Key, T> &&other)

As the Qt containers all support COW copies, they are cheap but they are not completely free. So, the move semantics were added for additional optimization.

Another change which was done in Qt 5.4 was adding the support for C++11 reference qualifiers, a move-semantics-related feature we haven't yet discussed. The idea for that feature is to optimize away a copy in a situation like the following one:

auto lowerCaseStrg = QString::fromUtf8(data).toLower();

Here, fromUtf8() returns a temporary, which then will be used by toLower() only as a throwaway input. It would be nice if toLower() could reuse the memory allocated by the string constructor and perform the transformation in place. To be able to detect such situations in your code, C++11 added the reference qualifier syntax as shown in the next example:

class QString
{
...
QString toLower() const &; // normal, returns a copy
QString toLower() &&; // rvalue, can convert in place!
...
};

The & and && at the end of toLower() denote the context from which the method is called and define overloads depending on the reference type of the this pointer. When a method is called on a temporary (an rvalue reference), the overload with && will be chosen and the in-place optimization will be applied.

In Qt 5.4, many methods of QString (for example toUppertoLowertoLatin1, and so on), QByteArray (toUppertoLower), QImage (for example, convertToFormatmirrorrednormalized, and so on), and QVersionNumber had this optimization added.