C++ Logo

std-proposals

Advanced search

Re: [std-proposals] return if

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Tue, 13 Jan 2026 15:03:29 +0000
On Tue, Jan 13, 2026 at 2:45 PM Alejandro Colomar wrote:
>
> Isn't this exactly the same as the following?
>
> if (SomeFunction())
> return false;


If you're dealing with 'bool' then yes it's exactly the same. But consider:

    template<typename T>
    T Func(void)
    {
        return false if SomeOtherFunc();
    }

The above would be equivalent to:

    template<typename T>
    T Func(void)
    {
        decltype(SomeOtherFunc()) x = SomeOtherFunc();
        if ( static_cast<bool>(x) ) return !x;
    }

Let's say you're dealing with 128-Bit UUID's, so you're using
__uint128_t. If 'SomeOtherFunc' returns a valid UUID, then 'Func' will
return {00000000-0000-0000-0000-00000000000}.

Or another wild example would be if we were to amend 'std::vector' so
that conversion to bool checks if any element is non-zero, and
"operator!" makes a copy of the vector and applies "operator!" to each
element. So if "SomeOtherFunc" were to return:

    vector<int>{ 0, 1, 2, 3, 0, 1, 2, 3 }

Then 'Func' would return:

    vector<int>{ 1, 0, 0, 0, 1, 0, 0, 0 }

Just showing how hypothetically a programmer could write a class and
define their own "operator bool" and "operator!" to do whatever they
want, and make use of it with in conjunction with "return if".

Received on 2026-01-13 15:03:42