Lifetime of object which has vacuous initialization

后端 未结 3 780
夕颜
夕颜 2021-01-15 17:26

Current draft standard says (previous standards have similar wording) in [basic.life/1]:

The lifetime of an object or reference is a runtime property of

3条回答
  •  悲哀的现实
    2021-01-15 18:05

    You cannot begin the lifetime of a object unless the object has been created. And [intro.object]/1 defines the only ways in which objects can be created:

    An object is created by a definition (6.1), by a new-expression (8.3.4), when implicitly changing the active member of a union (12.3), or when a temporary object is created (7.4, 15.2).

    The object created by this definition is of type char[]. Therefore, that is the only object whose lifetime begins. And no other objects are created by this construct.

    To lend credence to this interpretation, the proposal for C++20 P0593 exists whose primary purpose is to allow that very declaration to implicitly create other such objects.


    Comments:

    The condition in (1.2) still bothers me. Why is it there?

    It is there because it cannot say "the initialization is complete" for an object that doesn't undergo initialization.

    suppose, that I have a new(obj) int afterwards. That clearly creates an int object. But before that, obj has obtained the necessary storage.

    No, the declaration of obj obtained storage for an object of type char[]. What obtains storage for the int object being created is new(obj). Yes, the placement-new expression obtains storage for the object that it creates. Just like a declaration of a variable obtains storage for the object it creates.

    Just because that storage happens to exist already doesn't mean it isn't being obtained.

提交回复
热议问题