C++ Logo


Advanced search

Re: Initialisers in ternary operators

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Fri, 11 Sep 2020 00:36:51 +0300
On Fri, 11 Sep 2020 at 00:18, Dmitry Dmitry via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>> You can probably already have a similar outcome with:
>> return [&] { auto It = Cont.find(42); return It == Cont.end() ? 0 :
>> It->second; }();
>> which is not that much longer.
> Yeah... Right.
> But what about consistency (between regular if()'s and ternary operators)?
> It just reminds me how initially, auto parameters were invented in lambdas and then they were "ported" to regular function.
> Ditto here, initially init-statements were introduced in regular if()'s, but kind-of-forgotten in ternary operators?

It's quite different. The left-hand side of a ternary operator is an
arbitrary expression with no initial hint that a ternary operator
will eventually follow. In the init-statement-and-condition of an
if-statement, we know that we're going to see either
an init-statement or a condition, we have context that tells both
human readers and computer parsers what to expect.
That's not the case for a ternary expression. Furthermore, it would be
bloody weird to introduce a statement-expression
(auto It = Cont.find(42); It == Cont.end())
only for ternary expression left-sides. Such statement-expressions are
a thing in GCC as extensions; if you want an expression
that contains statements, write a lambda.

Received on 2020-09-10 16:40:34