Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #122020

Re: ({

From Alan Mackenzie <acm@muc.de>
Newsgroups comp.lang.c++
Subject Re: ({
Date 2025-03-22 10:53 +0000
Organization muc.de e.V.
Message-ID <vrm4rl$2ue$1@news.muc.de> (permalink)
References <vrhmme$8v0$1@news.muc.de> <vrjgh8$1bqpn$1@dont-email.me>

Show all headers | View raw


Paavo Helde <eesnimi@osa.pri.ee> wrote:
> On 20.03.2025 20:27, Alan Mackenzie wrote:
>> Hello, C++.

>> I'm having some difficulty (amending Emacs's C++ Mode) reconciling the
>> two conflicting uses in C++ of ({.

>> Firstly, it is used as a "statement expression", a GCC enhancement also
>> found in C, allowing a more relaxed and natural way to write an
>> expression as the end result of a sequence of statements:

>>       ({ int y = foo (); int z;
>>          if (y > 0) z = y;
>>       else z = - y;
>>       z; })

>> .  I think this usage is very old.

>> Secondly, there's initialisation expressions like:

>>    void f4 (int a, int b, int c)
>>    {
>>      std::vector<ABC> abcList2(
>>        {{a+6,
>>          b+6,
>>          c+6}
>>        }
>>        );
>>    ....
>>    }

>> .  Here the ( on the std::vector line, together with the next {, can be
>> confused as a statement expression, though it's clearly not meant that
>> way.  I think this syntax is much newer than the other one, though I may
>> be wrong here.

> This braced initialization (called "List-initialization") was introduced 
> in C++11, it is standardized and in wide and growing use.

Yes, thanks.

>> In calculating the indentation for source lines in these constructs, the
>> ambiguity causes mis-indentation for one or the other of them.

>> Now to my question: how common is GCC's statement expression in the wide
>> world of C++ source code?  How much would be lost if I simply removed the
>> statement expression from C++ Mode's parsing functions?

> It appears the statement expressions are a gcc extension which does not 
> even compile in standard C++, and is probably not needed for anything in 
> C++ as there are better options like templated and inlined functions.
> In C there might be some usage case for it.

I'm not sure what you meant by templated functions here, but an inline
function has the disadvantage of fragmenting the code.  Rather than have
a few lines of code where they're used, you need to look somewhere else
to see what they do.

There are some uses of the statement expression in the (C) Linux kernel
sources.

> During the last 25 years I do not recall having encountered this feature 
> ever, and I do not even recall anyone bashing it. So I guess it could be 
> ditched pretty easily.

Thanks, that was what I thought.  But in the last couple of days, I might
have come up with a way to parse both of the ({ constructs without (too
much) ambiguity.

-- 
Alan Mackenzie (Nuremberg, Germany).

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

({ Alan Mackenzie <acm@muc.de> - 2025-03-20 18:27 +0000
  Re: ({ Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-21 09:58 +0100
    Re: ({ Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-22 16:14 +0100
  Re: ({ Paavo Helde <eesnimi@osa.pri.ee> - 2025-03-21 12:54 +0200
    Re: ({ Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2025-03-21 12:27 -0700
      Re: ({ Bonita Montero <Bonita.Montero@gmail.com> - 2025-03-22 10:09 +0100
        Re: ({ Alan Mackenzie <acm@muc.de> - 2025-03-22 11:26 +0000
      Re: ({ Alan Mackenzie <acm@muc.de> - 2025-03-22 11:00 +0000
    Re: ({ Alan Mackenzie <acm@muc.de> - 2025-03-22 10:53 +0000
      Re: ({ scott@slp53.sl.home (Scott Lurndal) - 2025-03-22 14:09 +0000
        Re: ({ Alan Mackenzie <acm@muc.de> - 2025-03-22 15:20 +0000
          Re: ({ scott@slp53.sl.home (Scott Lurndal) - 2025-03-22 15:53 +0000
      Re: ({ Paavo Helde <eesnimi@osa.pri.ee> - 2025-03-22 17:18 +0200
        Re: ({ David Brown <david.brown@hesbynett.no> - 2025-03-22 17:45 +0100
    Re: ({ Jakob Bohm <egenagwemdimtapsar@jbohm.dk> - 2025-04-01 06:59 +0200

csiph-web