C++ Logo

std-proposals

Advanced search

Re: Initialisers in ternary operators

From: Richard Hodges <hodges.r_at_[hidden]>
Date: Fri, 11 Sep 2020 15:28:24 +0200
On Fri, 11 Sep 2020 at 12:39, Garrett May via Std-Proposals <
std-proposals_at_[hidden]> wrote:

> Hm .. who said,(in the context of C++ talk): "don't be clever unless you
>> really have to"?
>
> I'm not really sure what that means?
>
> Either way, my personal preference would be in support of Richard Hodges's
> suggestion. I've found GCC's expression statements to be useful in the
> past, and I think standardising it would be a perfect solution for this. It
> avoids the weirdness of having an initializer only for ternary operators
> that Ville Voutilainen mentioned (due to the syntax of ternaries) whilst
> also providing a way for scope to yield a value.
>

All that remains now is for someone to write a paper, in which is laid out
the motivating use case, a demonstration that existing language features
are limiting productivity and an impact assessment.

There is already a compiler supporting the feature, so that's in the
paper's favour.

I suspect the biggest problems would be:
a) convincing everyone that the existing lambda syntax is not sufficient
b) convincing other compiler vendors to bother to implement the feature.


>
> On Fri, 11 Sep 2020 at 10:58, Dusan Jovanovic (DBJ) <dbj_at_[hidden]> wrote:
>
>> Hm .. who said,(in the context of C++ talk): "don't be clever unless you
>> really have to"?
>>
>> On Fri, 11 Sep 2020 at 11:24, Garrett May via Std-Proposals <
>> std-proposals_at_[hidden]> wrote:
>>
>>> if (auto i = m.find(x); i == m.end())
>>>> return nullptr;
>>>> else
>>>> return std::addressof(i->second);
>>>
>>>
>>> This works fine when returning from a function, but not as well when
>>> storing a local value.
>>>
>>> On Fri, 11 Sep 2020 at 10:15, Bo Persson via Std-Proposals <
>>> std-proposals_at_[hidden]> wrote:
>>>
>>>> On 2020-09-11 at 08:56, Richard Hodges via Std-Proposals wrote:
>>>> > GCC already has expression statements as an extension.
>>>> >
>>>> > I don't think it would hurt to standardise this into the language.
>>>> >
>>>> > https://godbolt.org/z/sb63dz
>>>> >
>>>> > e.g.:
>>>> > return ({
>>>> > auto i = m.find(x) ;
>>>> > i == m.end() ? nullptr : std::addressof(i->second);
>>>> > });
>>>> >
>>>>
>>>> What's the supposed advantage of using a ternary operator here?
>>>>
>>>> Note that gcc/godbolt produces *exactly* the same machine code for
>>>>
>>>>
>>>> if (auto i = m.find(x); i == m.end())
>>>> return nullptr;
>>>> else
>>>> return std::addressof(i->second);
>>>>
>>>>
>>>>
>>>> >
>>>> > On Fri, 11 Sep 2020 at 09:19, Dmitry Dmitry via Std-Proposals
>>>> > <std-proposals_at_[hidden] <mailto:
>>>> std-proposals_at_[hidden]>>
>>>> > wrote:
>>>> >
>>>> > Just write and use a find wrapper like these:
>>>> >
>>>> >
>>>> > It was just an example.
>>>> > If we start considering solving it via wrappers/helpers, then I
>>>> > would just create something like findOr(key, value) function.
>>>> >
>>>> > But, anyway, it seems that the idea is not worth it...
>>>> > Thanks for the feedback.
>>>> >
>>>> > --
>>>> > Dmitry
>>>> > *_Sent from gmail_*
>>>>
>>>> --
>>>> Std-Proposals mailing list
>>>> Std-Proposals_at_[hidden]
>>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>>
>>> --
>>> Std-Proposals mailing list
>>> Std-Proposals_at_[hidden]
>>> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>>>
>> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>


-- 
Richard Hodges
hodges.r_at_[hidden]
office: +442032898513
home: +376841522
mobile: +376380212

Received on 2020-09-11 08:32:06