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


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

Re: ({

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c++
Subject Re: ({
Date 2025-03-22 17:45 +0100
Organization A noiseless patient Spider
Message-ID <vrmpft$a537$1@dont-email.me> (permalink)
References <vrhmme$8v0$1@news.muc.de> <vrjgh8$1bqpn$1@dont-email.me> <vrm4rl$2ue$1@news.muc.de> <vrmkd2$67va$1@dont-email.me>

Show all headers | View raw


On 22/03/2025 16:18, Paavo Helde wrote:
> 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; });
> 

It can also combine well with type inference, such as the example from 
GCC's reference:

#define max(a, b) \
	({ __auto_type _a = (a); __auto_type _b = (b); \
	  _a > _b ? _a : _b; })

(In C23, "auto" is approximately equivalent to "auto" in C++, so you 
don't need the gcc "__auto_type" extension any more.)

Of course such function-like macros are usually best replaced with 
template functions in C++.  I can't think off-hand of a situation where 
statement expressions in C++ would be significantly better than either 
an inline function / function template, or an immediately-invoked lambda.


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