C++ Logo

std-proposals

Advanced search

Re: [std-proposals] return if

From: Alejandro Colomar <une+cxx_std-proposals_at_[hidden]>
Date: Tue, 13 Jan 2026 16:20:29 +0100
Hi Frederick,

On Tue, Jan 13, 2026 at 03:03:29PM +0000, Frederick Virchanza Gotham via Std-Proposals wrote:
> 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;
> }

So, you're suggesting a feature where 'return false' doesn't really
mean 'return false' but actually 'return !x', and then overloading the
operator! can result in crazy behavior that one would never have
expected, because the code says 'return false'.

>
> 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 }

Is this not obviously wrong to you?

Remember you're suggesting this behavior for

 return false if ...;

What happened with that 'false'? Is this an official overload of the
'false' keyword?


Cheers,
Alex

> 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".

-- 
<https://www.alejandro-colomar.es>

Received on 2026-01-13 15:20:39