C++ Logo

std-proposals

Advanced search

Re: Initialisers in ternary operators

From: Ronan Keryell <rkeryell_at_[hidden]>
Date: Thu, 10 Sep 2020 21:06:03 +0000
On 9/10/20 1:36 PM, Dmitry Dmitry via Std-Proposals wrote:

> Hi,

Hello!

> Since C++17 we can have init-statements in if:
> if (_*char buf[10]*;_ std::fgets(buf, 10, stdin)) { m[0] += buf; }
>
> That is excellent. But what about ternary operators (?:)?
> It seems I cannot write something like this now:
> return (_*auto It = Cont.find(42)*;_ It == Cont.end()) ? 0 : It->second;
>
> Is it deliberate?
> Are there any objections?
> What do you think?

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.
--
Ronan KERYELL, Xilinx Research Labs / San José, California.

Received on 2020-09-10 16:09:37