C++ Logo

std-proposals

Advanced search

Re: [std-proposals] return if

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Mon, 12 Jan 2026 15:13:57 +0000
On Mon, Jan 12, 2026 at 1:11 PM Thiago Macieira wrote:
>
> Yes, but why do we need two syntaxes to do the same thing? In Perl, for any
> non-trivial return if, I end up indenting the if:
>
> return $somevar
> if $a > 0 and !$b;
>
> In Perl, a leading if requires the statement be surrounded by brackets, so the
> above is one line shorter. In C++, it wouldn't be.
>
> So why should we spend time changing the core language syntax?


As I try to think back now over different programs and different
projects, embedded microcontrollers and desktop PC's, I've written
code loads of times where the callee returns either:

    (1) void
    (2) bool
    (3) some kind of handle

The void's are pretty simple:

    void Func1(int a, double b)
    {
        if ( IsOutOfRange(a) ) return;
        if ( IsOutOfRange(b) ) return;
        DoSomething(a, b);
    }

There isn't much to gain from allowing the above to be changed to:

    void Func1(int a, double b)
    {
        return if IsOutOfRange(a);
        return if IsOutOfRange(b);
        DoSomething(a, b);
    }

Now let's consider the bool's:

    bool Func2(int a, double b)
    {
        if ( ! InRange(a) ) return false;
        if ( ! InRange(b) ) return false;
        return (double)a > b;
    }

This could be turned into:

    bool Func2(int a, double b)
    {
        return if ! InRange(a);
        return if ! InRange(b);
        return (double)a > b;
    }

I already explained that when the compiler sees "return if expr", it
does the following:

    decltype(expr) var = expr;
    if ( static_cast<bool>(var) ) return var;

So you're probably wondering what does "return if ! expr" do? If you
place an exclamation mark after "return if", the processing changes
to:

    if ( false == static_cast<bool>(var) ) return var;

Now let's consider something a little more complicated:

    Handle Func3(int a, double b)
    {
        if ( Handle h = Try_Get_RS232_Handle(a,b) ) return h;
        if ( Handle h = Try_Get_OSDP_Handle(a,b) ) return h;
        return CreateNewGenericHandle();
    }

which could become:

    Handle Func3(int a, double b)
    {
        return if Try_Get_RS232_Handle(a,b);
        return if Try_Get_OSDP_Handle(a,b);
        return CreateNewGenericHandle();
    }

This is where it gets handy and very readable, not needing to name a
variable and mention its name twice.

In your own code example, Thiago, you want to check the expression's
bounds before deciding whether to return. I'm not saying that that
doesn't happen and that we don't write code like that -- but I think 9
times out of 10 it's a simple "return the expression if the expression
is true after being static_cast'ed to bool".

The new feature "return if" won't always be suitable, but it will be a
lot of the time. Here are a few examples of where it would not be
useful:
A) Where you need to check bounds, for example in Perl: return
$somevar if $a > 0 and !$b;
B) Where the invalid value isn't false when converted to bool (e.g.
MS-Windows has INVALID_HANDLE defined as "((HANDLE)(LONG_PTR)-1)")
C) Where you need to invert the value, for example " if ( IsOpen() )
return false; "

With regard to A, I don't place to solve that with "return if".
With regard to B, Microsoft should be given a rugby ball and be
allowed to go play outside in the mud.
With regard to C, maybe there could be fancy syntax for this.

Received on 2026-01-12 15:14:10