Nominate our 2022 Qt Champions!

Member function empty() missing in some containers

  • QList<> has empty(); QString and QByteArray do not. This makes it difficult to write portable code where these containers are actually typedef'd as either QString or std::string, QByteArray or std::vector<char> depending on whether or not the program is using Qt.

    Of course, we can use size(), but this is sometimes not very efficient if there is a lot of data in a vector or string. Scott Meyers, for example, recommends calling empty() if we don't need (or want) to know the actual size of a container's data.

    Would this break a lot of existing code if Qt added empty() to these containers?

  • I saw that QVector also has empty(), and it also guarantees that its elements are adjacent in memory. I will use it to replace std::vector.

Log in to reply