Skip to content

[dcl.init.general] Could the definition of value-initialization be simplified? #5084

Description

@cpplearner

[dcl.init.general]:

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type ([class]), then
    • if T has either no default constructor ([class.default.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;
    • otherwise, the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;
  • [...]

It seems that if T has no default constructor, then it can't meet the semantic constraints for default-initialization, and the "otherwise" branch correctly makes it ill-formed. (It doesn't matter whether zero-initialization is performed or not, since the initialization is ill-formed either way.)

CWG 1301 adds the "no default constructor" part. When CWG 1301 was adopted, the "otherwise" branch was not specified to check the semantic constraints for default-initialization (which was changed later by CWG 1507). Now that the "otherwise" branch does the right thing, I think it's not necessary to handle the "no default constructor" in the "if" branch.

Suggestion:

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type ([class]), then
    • if T has either no default constructor ([class.default.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;
    • otherwise, the object is zero-initialized and the semantic constraints for default-initialization are checked, and if T has a non-trivial default constructor, the object is default-initialized;
  • [...]

Activity

  1. jensmaurer commented on Nov 3, 2021

    @jensmaurer
    Member

    I have a mild preference for keeping the status quo:

    • With the change, objects without a default constructor are zero-initialized before giving an error.
    • "semantic constraints for default init" is intended to perform template instantiations as needed; adding the "no default ctor" case on top of that rather vague phrase doesn't feel like an improvement.
  2. jensmaurer commented on Nov 3, 2021

    @jensmaurer
    Member

    @zygoloid , any opinion here?

  3. frederick-vs-ja commented on Aug 7, 2024

    @frederick-vs-ja
    Contributor

    Seems superseded by resolution of CWG2859. Should we consider this fixed by #7099 (2785faa)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions