llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.98k stars 11.95k forks source link

C++20 Designated Initializers - Nested initialization #47039

Open llvmbot opened 4 years ago

llvmbot commented 4 years ago
Bugzilla Link 47694
Version trunk
OS All
Reporter LLVM Bugzilla Contributor
CC @pogo59,@zygoloid

Extended Description

Hi,

This code snippet is taken from P0329R4.

struct A
{
    int x;
};

struct B
{
    A a;
};

int main()
{
    B b{.a.x = 0};
    return 0;
}

GCC fails to compile this example, which should be invalid according to the C++20 standard.

GCC error:

error: expected primary-expression before '.' token
   13 |     B b{.a.x = 0};
ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 4 years ago

(But see also bug 47693 comment 1: -Werror=c99-designator isn't enough at the moment to turn off all the C99-but-not-C++20 cases.)

ec04fc15-fa35-46f2-80e1-5d271f2ef708 commented 4 years ago

Clang intentionally accepts the full C99 designated initializer syntax in C++ as a longstanding language extension (predating C++20 designated initialization).

If you want this extension to be disabled, you can use -Werror=c99-designator. If you want all such extensions disabled, you can use -pedantic-errors.

llvmbot commented 4 years ago

I believe C does allow these.

pogo59 commented 4 years ago

Does C allow these? It's been too long, I can't remember the details.