const T{}; works, const T; fails when T is a non-POD,

后端 未结 2 770
半阙折子戏
半阙折子戏 2020-12-06 09:44

To start with, I have a struct with one value with a default value

struct S {
    int a = 1;
};

This type can be default constructed when i

2条回答
  •  既然无缘
    2020-12-06 10:19

    So it looks like gcc is basing this on DR 253 even though this is not resolved yet. We can see this from the the following gcc bug report which says:

    This is by design, because as DR 253 shows, the normative standard is flawed.

    and the gcc change that brought this into effect says:

    Core 234 - allow const objects with no initializer or user-provided default constructor if the defaulted constructor initializes all the subobjects.

    So technically clang is correct and gcc is not conformant but it seems like they believe DR 253 will be resolved in their favor. This makes complete sense if the main concern is indeterminate initial value which as far as I can tell it is. This change is documented in gcc 4.6 release notes:

    In 4.6.0 and 4.6.1 G++ no longer allows objects of const-qualified type to be default initialized unless the type has a user-declared default constructor. In 4.6.2 G++ implements the proposed resolution of DR 253, so default initialization is allowed if it initializes all subobjects. Code that fails to compile can be fixed by providing an initializer e.g.

    struct A { A(); };
    struct B : A { int i; };
    const B b = B();
    

提交回复
热议问题