Re: ({

Liste des GroupesRevenir à cl c++ 
Sujet : Re: ({
De : eesnimi (at) *nospam* osa.pri.ee (Paavo Helde)
Groupes : comp.lang.c++
Date : 22. Mar 2025, 16:18:58
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vrmkd2$67va$1@dont-email.me>
References : 1 2 3
User-Agent : Mozilla Thunderbird
On 22.03.2025 12:53, Alan Mackenzie wrote:
Paavo Helde <eesnimi@osa.pri.ee> wrote:
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.
At the GCC page for statement expressions they say "This feature is especially useful in making macro definitions “safe” (so that they evaluate each operand exactly once)" and most of their examples are about macros.
A macro is fragmenting the code exactly in the same way as an inline function, except the macros are worse than functions in multiple ways. In C one might argue macros are needed for supporting multiple types at the same time, but in C++ this is solved much better by function templates.
Ergo, a C-style macro using statement expressions can easily replaced with a more regular and better behaving function or function template in C++, with no increase in code "fragmentation".
For "in-place" use Bonita is right a lambda can be used to the same effect, but the readability probably does not go better. As per the first example on the GCC page (poor man implementation of abs()):
int a = ({ int y = foo (); int z; if (y > 0) z = y; else z = - y; z; });
can be written with a lambda in C++:
int a = [](){int y = foo (); int z; if (y > 0) z = y; else z = - y; return z; }();
I suspect Bonita might like this. I still prefer the more mundane
int a = std::abs(foo());

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