Do I really need to implement user-provided constructor for const objects?

前端 未结 4 1987
粉色の甜心
粉色の甜心 2020-11-27 18:45

I have the code:

class A {
  public:
    A() = default;

  private:
    int i = 1;
};

int main() {
  const A a;
  return 0;
}

It compiles

4条回答
  •  無奈伤痛
    2020-11-27 19:17

    Edit: The following is based on outdated information. I just went through N3797 and this is what I found:

    § 8.5/7 [dcl.init]
    If a program calls for the default initialization of an object of a const-qualified type T, T shall be a class type with a user-provided default constructor.

    Note the standard quote in the link below says user-declared.


    The following program compiles in g++ but not clang++:

    struct A {};
    
    void f()
    {
      A const a;
    }
    

    And it might be related to this bug report where it was "fixed". g++ fails to compile it once it contains data members unless they're initialized. Note that int member = 1 will no longer make A a POD. Comparatively, clang++ rejects all permutations (empty classes and data members initialized or not.) For an interpretation of what the standard means by the following paragraph:

    § 8.5/9 [dcl.init] says:

    If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for an object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.

    See Why does C++ require a user-provided default constructor to default-construct a const object?. Supposedly the program is ill-formed if the object is of const-qualified POD type, and there is no initializer specified (because POD are not default initialized). Note how g++ behaves for the following:

    struct A {int a;};
    struct B {int a = 1;};
    int main() 
    {
        A a;
        B b;
        const A c; // A is POD, error
        const B d; // B is not POD, contains data member initializer, no error
    }
    

提交回复
热议问题