Skip to content

ES.107 (and others) never mentions std::ssize() #2294

@jarzec

Description

@jarzec

At the time of writing nowhere within the Guidelines is there a single mention of the std::ssize() function added in C++20.
I might be mistaken but my understanding is that this addition to the standard library solves many of the issues with the standard containers using unsigned subscripts, or specifically the unsigned return type of the .size() method.

I have mentioned the rule ES.107 in the title as I would see the example code there a perfect place to use std::ssize().
The Enforcement section states that enforcement of ES.107 is tricky and gives the example of comparison of gsl::index with the results of .size() and sizeof. While not much can be done about sizeof the comparison issues of gsl::index with .size() are entirely (?) removed by the use of std::ssize().
Maybe sizeof is waiting for a mechanism analogous to std::ssize() 😉?

The reason I am raising this question is that I have been suggesting the use of std::ssize() in my code reviews for a while but I could not support this with anything in the Guidelines. It was relatively easy to justify, though, as it was mainly protecting against the type of issues that can arise with
my_container.size() - 1
as opposed to
std::ssize(my_container) - 1.

I believe that multiple places within the Guidelines could employ std::ssize() - many (but not all) of those that currently use .size().
Possibly even a dedicated rule for accessing sizes of standard containers with std::ssize(), if C++20 is available, could be added?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions