C++ Logo

std-proposals

Advanced search

Re: [std-proposals] return if

From: David Brown <david.brown_at_[hidden]>
Date: Thu, 15 Jan 2026 15:00:18 +0100
On 15/01/2026 11:45, Frederick Virchanza Gotham via Std-Proposals wrote:
> On Thu, Jan 15, 2026 at 9:54 AM Filip via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>>
>> return if appears to be basically an assert that does not stop your program from running.
>>
>> Maybe it is better to just use something similar to assert?
>>
>> ```
>> 1. soft_assert(foo());
>> 2. soft_assert(foo(), default_return_value);
>> ```
>
>
> No that's not the intention.
>
> Imagine you have a function that retrieves the UUID for a given
> signatory on a bank account, and the bank operates in 3 different
> countries:
>
> __uint128_t GetSignatoryID( chrono::time_point date_of_birth,
> string_view primary_surname_on_birthcert, string_view
> primary_forename_on_birthcert )
> {
> return if manager.GetRegion("Sweden" ).FindRecord(
> date_of_birth, primary_surname_on_birthcert,
> primary_forename_on_birthcert );
> return if manager.GetRegion("Germany").FindRecord(
> date_of_birth, primary_surname_on_birthcert,
> primary_forename_on_birthcert );
> return if manager.GetRegion("Japan" ).FindRecord(
> date_of_birth, primary_surname_on_birthcert,
> primary_forename_on_birthcert );
>
> return GenerateNewID( date_of_birth,
> primary_surname_on_birthcert, primary_forename_on_birthcert );
> }
>


__uint128_t GetSignatoryID( chrono::time_point date_of_birth,
 string_view primary_surname_on_birthcert,
 string_view primary_forename_on_birthcert )
{
 for (auto region : { "Sweden", "Germany", "Japan" } ) {
  if (auto x = manager.GetRegion(region).FindRecord(
   date_of_birth,
   primary_surname_on_birthcert,
   primary_forename_on_birthcert)) return x;
 }
 return GenerateNewID( date_of_birth,
  primary_surname_on_birthcert,
  primary_forename_on_birthcert );
}

If you find it inconvenient to write the same long code repeatedly,
start by picking sane names for identifiers. Then use loops, or perhaps
a lambda, to avoid the repetition.

I have yet to see an example of "return if" where I think it would be a
noticeable improvement, even if its semantics were clear and unambiguous
(which they are not).

Received on 2026-01-15 14:00:26