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 17:51:46 +0000
On Thursday, October 9, 2025, Sebastian Wittmeier via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Wouldn't that be terrible?
>
>
>
> To even cast function pointers?
>


If we forget about function pointers for a minute, we could make it just
about integer expressions.
So if you apply +|- to an integer expression, then you get the equivalent
of:

    template <typename T>
    requires std::is_integral_v<T>
    constexpr std::make_signed_t<T> to_signed(T const value)
    {
        constexpr int bits = std::numeric_limits<T>::digits;

        return
            value <= std::numeric_limits< std::make_signed_t<T> >::max()
            ? value
            : value - (T{1} << bits);
    }

Or even better.... if the programming language allowed us to define our own
trigraph or quadgraph unary operators, then we could define the template
function first and then define the operator afterward:

    operator +|- to_signed;

Perhaps even allow the programmer to set the precedence of the operator,
for example:

    operator +|- precedence(3) to_signed;

This would make C++ really ergonomic when dealing with fancy libraries that
do very advanced mathematics.

Received on 2025-10-09 17:51:49