Search code examples
c++qtqt5

Returning empty list to QImage


While browsing through some Qt code, I came across a function that is defined with a return type of QImage, but it's return value is an empty string {}. What does it mean? Couldn't figure it out from the searching I did.

Example:

QImage ExampleRenderer::addImage(QuickMapGL *mapItem, const QString &iconId)
{
    if (mapItem == nullptr) {
        return {};
    }
    const QImage image = iconProvider->requestImage(iconId, nullptr, QSize());
    if(image.isNull()){
        return {};
    }
    //
    ...
}

Solution

  • Modern C++ requires the compiler to deduce the (still static compile time) type without you needing to type it out. return is one of these cases, it's same as QImage{}, which is same as older style QImage(), meaning default constructed QImage value.

    Here is some discussion on this {} initialization, called Uniform Initialization, and why it was added to C++.