Title
Why must empty or fully-initialized const objects be initialized?
Status
c++17
Section
9.4 [dcl.init]
Submitter
Mike Miller

Created on 2000-07-11.00:00:00 last changed 83 months ago

Messages

Date: 2011-08-15.00:00:00

Notes from the August, 2011 meeting:

If the implicit default constructor initializes all subobjects, no initializer should be required.

Date: 2011-02-15.00:00:00

Additional note (February, 2011):

This issue should be brought up again in light of constexpr constructors and non-static data member initializers.

Date: 2016-11-15.00:00:00

[Adopted at the November, 2016 meeting as part of paper P0490R0.]

Paragraph 9 of 9.4 [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.

What if a const POD object has no non-static data members? This wording requires an empty initializer for such cases:

    struct Z {
        // no data members
        operator int() const { return 0; }
    };

    void f() {
        const Z z1;         // ill-formed: no initializer
        const Z z2 = { };   // well-formed
    }

Similar comments apply to a non-POD const object, all of whose non-static data members and base class subobjects have default constructors. Why should the class of such an object be required to have a user-declared default constructor?

(See also issue 78.)

History
Date User Action Args
2018-02-27 00:00:00adminsetstatus: drafting -> c++17
2011-09-06 00:00:00adminsetmessages: + msg3531
2011-09-06 00:00:00adminsetstatus: open -> drafting
2011-02-28 00:00:00adminsetmessages: + msg3251
2000-07-11 00:00:00admincreate