[Bug c++/97665] constexpr union array member incorrectly rejected as non-constexpr

ldalessandro at gmail dot com gcc-bugzilla@gcc.gnu.org
Mon Nov 2 17:30:43 GMT 2020


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97665

--- Comment #4 from Luke Dalessandro <ldalessandro at gmail dot com> ---
Hi Jakob,

Thank you for looking at this. I restructured the code sample according to your
suggestions and it is available here https://godbolt.org/z/P1bMEz. I don't
understand a couple of things that you said, and ultimately I'm not sure if you
are agreeing that this could be a bug.

> You can do Foo foo = Foo(); and it compiles.
1. I can't do `Foo foo = Foo();` because the purpose of the union is to
allocate uninitialized storage for the `Foo` during `constexpr` execution when
`Foo` has no default constructor. I realize now I meant to write `constexpr
Foo() = delete;` in the original code. I _can_ use the monostate to have an
active member at initialization, but would prefer not to as it complicates the
union. 

> clang++ rejects it too:
> error: constexpr union constructor that does not initialize any member is a 
> C++20 extension [-Werror,-Wc++20-extensions] though only with -pedantic-errors.
2. I can't get clang to emit the warning you describe even with the provided
flags, perhaps I am doing it wrong or misinterpreting your comment.

> P1331R2 support is there since 
> r10-5194-g7906797ebec6881d7d90165340f51efcf447d716 
> (so without [1] it is accepted for -std=c++2a since that revision)
3. I think this means that, if the member is not an array, then it is accepted
with `-std=c++2a`. I do observe this, however it's not my use case. Technically
I have an array of some class template parameter `N`.

The updated test case with the deleted constructor is.

```
struct Foo {
    constexpr Foo() = delete;
};

union U {
    // struct {} monostate = {};
    Foo foo;
    constexpr U() {}
};

struct V {
    U storage[1];
};

constexpr V v;
```


More information about the Gcc-bugs mailing list