C++ Logo

std-proposals

Advanced search

Re: [std-proposals] return if

From: David Brown <david.brown_at_[hidden]>
Date: Thu, 15 Jan 2026 11:31:57 +0100
On 15/01/2026 10:20, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Thu, Jan 15, 2026 at 8:59 AM Ville Voutilainen wrote:
>>
>> On Thu, 15 Jan 2026 at 10:52, Frederick Virchanza Gotham via
>> Std-Proposals <std-proposals_at_[hidden]> wrote:
>>> because the following fails to compile:
>>>
>>> struct C {
>>> operator bool(void) const & = delete;
>>
>> ..as if someone writes code like that, or in any significant numbers.
>>
>> The idea continues to have insufficient merit, and isn't an
>> improvement over what we already have.
>
>
> It's funny, I feel as though the argument you and Thiago are making
> against my idea, is actually an argument in favour of my idea.
>
> Essentially what we are trying to do is mimic the behaviour of
> "return CalleeFunction();".
>
> But the complication is that we only want to return from the enclosing
> function if the callee's return value becomes true when converted to
> bool. So our first attempt is:
>
> if ( auto x = CalleeFunction() ) return x;
>
> In this thread I have demonstrated how mimicking the behaviour of
> "return CalleeFunction();" is actually a lot more complicated, as we
> need to do:
>
> if ( decltype(auto) x = CalleeFunction();
> static_cast<decltype(x)>(x) ) return static_cast<decltype(x)>(x);

No, we do /not/ need to do that.

if ( auto x = CalleeFunction() ) return x;

is clear, simple, unambiguous, and works /today/ in virtually every
case. It is only in some very niche - or directly pathological -
situations when you need something more. And the user will usually know
they are in such a situation, and will want to be clear and explicit
about any decltype usage needed to make different types of references to
work correctly. The last thing anyone needs or wants is to have such
things hidden inside a new rarely useful and counter-intuitive construct.

If you think "static_cast<decltype(x)>(x)" is something that C++
programmers regularly have to write, then here is your chance to do
something actually useful for the language. Figure out /why/ people
have to write that, and in what circumstances. Then figure out a way
that this could be avoided or simplified, and write a proposal on that.



>
> This really shows that it's much better to leave it up to the compiler
> rather than rely on the programmer to do it right. And here's how you
> ask the compiler to do it right:
>
> return if CalleeFunction();
>
> So thank you Thiago and Ville for supporting my idea with your
> argument in favour of it.

Received on 2026-01-15 10:32:07