Date: Thu, 19 Oct 2023 19:26:04 +0200
On 19/10/2023 12.05, Alex Rozenman via Std-Proposals wrote:
> 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
> }
I find
> 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
> }
I find
---- if (not vect.empty()) { // vector is not empty } ---- more expressive than using !, which might be easy to overlook. > > > 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 > } And I personally find this less expressive than querying for empty, or the size. Explicit conversions tend to make the code more readable. Plus it might generate confusion with pointers... if (vect and *vect){ } Also I might not sure if the feature is worth it. Unless one wants to get the first element (or an element in a specific position), working with empty and non-empty containers normally does not require special handling. > > 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] <mailto:rozenman_at_[hidden]>). >
Received on 2023-10-19 17:26:09