C++ Logo

std-proposals

Advanced search

Re: [std-proposals] if !(condition)

From: Thiago Macieira <thiago_at_[hidden]>
Date: Sat, 20 Apr 2024 18:42:48 -0700
On Saturday 20 April 2024 09:09:44 GMT-7 Yexuan Xiao via Std-Proposals wrote:
> I've wrote a proposal that suggests allowing the omission of the outermost
> parentheses to simplify the conditions in if statements:
> https://storage.nykz.org/proposals/if-not/
>
> Please share your comments.

Your text says that any conditional expression is valid. That seems to be
intended to allow things like:

 if !a.empty() b();

But it would allow use of anything in that expression, such as:

 if !a.empty && b.empty() b();

Which is fine because it's unambiguous, but how about

 if !a.empty() + b.empty() - b();

The + and - operators can be unary or binary, so the above could be
interpreted as either:
 if (!(a.empty() + b.empty()))
     -b();
or
 if (!a.empty())
     +b.empty() - b();

or
 if (!(!a.empty() + b.empty() - b()))
     ;

Which one is it? Without further limitations that you didn't include in your
proposal, it will be the third option.

In turn, this means you cannot apply this new syntax to every negated
expression because it would change the meaning of if blocks with a single
statement. You'd need to either keep the parentheses or to add curly brackets
to the statement, which negate the benefit.

Therefore, I also call into question your estimation of how many conditionals
could have used this in the first place.

PS: I'm assuming you'll remove the init-statement because that creates even
more ambiguity.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Principal Engineer - Intel DCAI Cloud Engineering

Received on 2024-04-21 01:42:55