C++ Logo

std-proposals

Advanced search

Re: [std-proposals] return if

From: Muhammad <mail_at_[hidden]>
Date: Mon, 12 Jan 2026 09:25:15 +0000
What about the following syntax

return <expression> if <condition>;

it should give the same effect and it syntax is more concise.

------ Original Message ------
>From "Jonathan Wakely via Std-Proposals"
<std-proposals_at_[hidden]>
To "C++ Proposals" <std-proposals_at_[hidden]>
Cc "Jonathan Wakely" <cxx_at_[hidden]>
Date 1/12/2026 11:12:55 AM
Subject Re: [std-proposals] return if

>See P2561
>
>On Mon, 12 Jan 2026, 00:00 Frederick Virchanza Gotham via
>Std-Proposals, <std-proposals_at_[hidden]> wrote:
>>Consider the following generic line of code:
>>
>> if ( handle const h = CreateHandle( 0xff ) ) return h;
>>
>>Right now I'm patching the GNU g++ compiler, inside the file
>>"gcc/cp/typeck.cc", inside the function
>>"finish_class_member_access_expr" because I want it to return early if
>>it's dealing with a chimeric_ptr. However if it's not dealing with a
>>chimeric_ptr, I want it to keep going. So right now the line of code
>>I'm adding looks like this:
>>
>> if ( tree const t = check_for_chimera(name, object) ) return
>>t;
>>
>>To be very generic about what the above line of code is doing:
>> (1) It invokes a function (let's call it the callee function).
>> (2) If the callee's return value after conversion to bool yields
>>true, then the callee's return value is immediately returned from the
>>enclosing function.
>> (3) If the callee's return value after conversion to bool yields
>>false, the enclosing function keeps going.
>>
>>So to break it down logically, here's what's going on:
>>
>> auto const retval = SomeFunction();
>> if ( static_cast<bool>(retval) ) return retval;
>>
>>Now personally I think that this would be much more succinctly
>>expressed as follows:
>>
>> return if SomeFunction();
>>
>>And so if we take another look at the line of code I'm adding to the
>>GNU g++ compiler:
>>
>> if ( tree const t = check_for_chimera(name, object) ) return
>>t;
>>
>>It could become:
>>
>> return if check_for_chimera(name, object);
>>
>>I can think of loads of times over the past 20+ years when I've coded
>>a function that does exactly this -- it returns immediately if the
>>callee succeeded, or it keeps going if the callee failed. This can be
>>simplified with "return if". And you'll see the benefit of it when you
>>have multiple function calls:
>>
>>Symbol *resolve_symbol(Name &name, Scope &scope)
>>{
>> return if try_fast_path(name, scope);
>> return if try_cached(name, scope);
>> return if try_builtin_table(name);
>>
>> // Slow path:
>> . . .
>>}
>>
>>This feature is about readability and convenience.
>>--
>>Std-Proposals mailing list
>>Std-Proposals_at_[hidden]
>>https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2026-01-12 09:25:38