C++ Logo


Advanced search

Re: [wg14/wg21 liaison] Designated initializers in C++ and C

From: Jens Gustedt <jens.gustedt_at_[hidden]>
Date: Thu, 13 Aug 2020 09:42:36 +0200

on Thu, 13 Aug 2020 00:01:08 +0200 you (Jens Maurer
<Jens.Maurer_at_[hidden]>) wrote:

> On 12/08/2020 21.37, Jens Gustedt via Liaison wrote:
> > Since it seems that designated initializers are new in C++20, had
> > that particular property much been used before?
> Assuming you refer to defined ordering in something like
> int x = 0;
> int a[] = { x++, x++, x++ }; // 3 elements with values 0, 1, 2
> (with no designated initializers): yes, C++ code relies on it.
> The guaranteed ordering is used heavily when handling variadic
> templates iteratively (instead of recursively), for example
> template<class ... T>
> void g(T...args)
> {
> int dummy[] = { (f(args), 0) ... };
> }
> Here, we want to call f for each argument, in order.

Ok, good to know. I will add something along the lines to my document
about the intersection of C and C++. It would go along the lines

      C and C++ differ on an important aspect concerning
      initializers. Where for C the expressions in an initializer are
      unsequenced, C++ imposes specification of the initializers in
      declaration order and sequences them according to that order. As
      a consequence

      - providing designated initializers in an order that is
        different from declaration order is a constraint violation in

      - in C, using expressions that must be sequenced to be valid
        (such as multiple occurence of the same increment operation)
        leads to undefined behavior.

      It is recommended that applications that target the common C/C++
      core list initializers in declaration order. Further it is
      recommended that implementations that target that core diagnose
      situations that would be problematic for the other language,
      such as initializers not appearing in declaration order or
      initializer expressions that require sequencing.

I am not too optimistic we can change this situation, though. The fact
that initializer expressions and function argument evaluation are
unsequenced in C is deeply built into optimizers, I think.

Jens (the other one)

:: INRIA Nancy Grand Est ::: Camus ::::::: ICube/ICPS :::
:: ::::::::::::::: office Strasbourg : +33 368854536   ::
:: :::::::::::::::::::::: gsm France : +33 651400183   ::
:: ::::::::::::::: gsm international : +49 15737185122 ::
:: http://icube-icps.unistra.fr/index.php/Jens_Gustedt ::

Received on 2020-08-13 02:46:32