Re: ({

Liste des GroupesRevenir à cl c++ 
Sujet : Re: ({
De : acm (at) *nospam* muc.de (Alan Mackenzie)
Groupes : comp.lang.c++
Date : 22. Mar 2025, 11:53:41
Autres entêtes
Organisation : muc.de e.V.
Message-ID : <vrm4rl$2ue$1@news.muc.de>
References : 1 2
User-Agent : tin/2.6.4-20241224 ("Helmsdale") (FreeBSD/14.2-RELEASE-p1 (amd64))
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).


Date Sujet#  Auteur
20 Mar 25 * ({13Alan Mackenzie
21 Mar 25 +* Re: ({2Bonita Montero
22 Mar 25 i`- Re: ({1Bonita Montero
21 Mar 25 `* Re: ({10Paavo Helde
21 Mar 25  +* Re: ({4Keith Thompson
22 Mar 25  i+* Re: ({2Bonita Montero
22 Mar 25  ii`- Re: ({1Alan Mackenzie
22 Mar 25  i`- Re: ({1Alan Mackenzie
22 Mar 25  +* Re: ({4Alan Mackenzie
22 Mar 25  i+* Re: ({2Paavo Helde
22 Mar 25  ii`- Re: ({1David Brown
22 Mar 25  i`- Re: ({1Alan Mackenzie
1 Apr 25  `- Re: ({1Jakob Bohm

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal