C++ Logo

liaison

Advanced search

Re: [wg14/wg21 liaison] Array lvalues in braced initializer lists

From: will wray <wjwray_at_[hidden]>
Date: Sun, 1 Aug 2021 16:02:08 -0400
On Sun, Aug 1, 2021 at 2:56 PM Uecker, Martin via Liaison <
liaison_at_[hidden]> wrote:

> Would the following example change behavior?
>
> void foo(void)
> {
> void* b[3];
> void* a[1][3] = { b };
> }
>

Yes - I guess so.

Is the initialization of 'a' ill-formed in C++? g++ rejects it with:

 *error: *array must be initialized with a brace-enclosed initializer
    4 | const void* a[1][3] = { *b* };
      | *^*
(it's accepted by clang++ and msvc, though, in pedantic/non-permissive mode)
(on adding another brace it is accepted by all 3).

There's a similar case with array-of-bool:
    #include <stdbool.h>

void foo(void)
{
bool b[3];
bool a[2][3] = { b };
}

Now, gcc accepts with no warning (gcc -std=c2x -pedantic -Werror)
and clang rejects with -Werror because it diagnoses:

*error: **address of array 'b' will always evaluate to 'true'
      [-Werror,-Wpointer-bool-conversion]*
  bool a[2][3] = { b };
* ^*

Received on 2021-08-01 15:02:23