Sujet : Re: ({
De : egenagwemdimtapsar (at) *nospam* jbohm.dk (Jakob Bohm)
Groupes : comp.lang.c++Date : 01. Apr 2025, 05:59:03
Autres entêtes
Organisation : Privat
Message-ID : <vsfrqn$28p75$1@dont-email.me>
References : 1 2
User-Agent : Epyrus/2.1.3
On 2025-03-21 11:54, Paavo Helde 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.
Actually, it is from K&R C, except (perhaps) the option to put the list expression in a pair of regular parenthesis with the usual identical meaning. The basic K&R form is:
int smallprimes[4] = { 2, 3, 5, 7 };
int identitymatrix[2][2] = { { 1, 0 }, { 0, 1 } };
In ANSI C, these can be declared const and placed in a read-only part of the resulting program file. This was broken by the desire to allocate all data on the heap via heap-centric classes like vector<int> .
Enjoy
Jakob
-- Jakob Bohm, MSc.Eng., I speak only for myself, not my companyThis public discussion message is non-binding and may contain errorsAll trademarks and other things belong to their owners, if any.