C++ Logo

std-proposals

Advanced search

Re: [std-proposals] ranges::size should always return a unsigned integer

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Thu, 9 Oct 2025 11:43:41 +0000
On Wednesday, October 8, 2025, Yexuan Xiao wrote:

> Why is it allowed to return a signed integer? C++ users are accustomed to
> the fact that the size_type of containers is an unsigned integer, so the
> size of containers should also return an unsigned integer.
>



 - - - - - - - Begin Half-baked Idea - - - - - - -


What if we could define a function (or member function) as follows:

    +|- int size(void);

The trigraph, +|- , at the beginning indicates that the function has two
forms: one returning signed, and one returning unsigned.

So then when you want to invoke the function, you get the unsigned version
as follows:

    auto x = size();

and you get the signed version as follows:

    auto x = +|- size();

Similarly you would ake the address of the function as follows:

    int (*p1)(void) = &size;
    unsigned (*p2)(void) = & +|- size;

I would add the stipulation that both of these function pointers can
compare equal (because they'll most likely have the same machine code
anyway since Two's Complement is now mandatory).

Received on 2025-10-09 11:43:44