C++ Logo

std-proposals

Advanced search

[std-proposals] Bool casting operator to check if a container not empty

From: Alex Rozenman <rozenman_at_[hidden]>
Date: Thu, 19 Oct 2023 13:05:45 +0300
In many cases, when it is needed to check if a container not empty,
"size()" or "empty()" functions are used:

if (vect.size() != 0) {

    // vector is not empty

}

if (!vect.empty()) {

    // vector is not empty

}


It seems that both variants are not expressive enough, so an explicit
function may be required, like:

if (vect.not_empty()) {

    // vector is not empty

}


In this message I would like to suggest to use of a bool casting operator
as the most clean form of this function:

if (vect) {

  // vector is not empty

}

The proposal seems to be backward compatible because until now there was no
bool casting operator defined in standard container classes.

(and it is not possible to define a global one).
-- 
Best regards,
Alex Rozenman (rozenman_at_[hidden]).

Received on 2023-10-19 10:05:58