Search code examples
c++constructorlanguage-lawyerterminologyobject-lifetime

Is object existence distinct from object lifetime?


It may sound philosophical, but it isn't: in C++, can various (classes, scalars) objects exist outside of their lifetime? What is the existence of an object? What is the creation of an object? Is an object created when its lifetime starts?

(Edited for clarity: the question is not specifically about class types.)

I'm extremely confused and need someone to explain the terminology and basic concepts to me.

Note: Existing is the fact of being a thing. It's the most fundamental philosophical concept. It isn't an attribute of an object and I don't know nor do I care about the number of occurrences of the word "exist" in the standard text. Textbooks probably very rarely say that things "exist". I don't remember ever reading that numbers "exist" in registers or that expressions "exist" in the source code. Numbers fit in registers and the source code has expressions in it.

If we can refer to an object, it means it exists. If a pointer points to an object, that object exists.


Solution

  • This is best understood with [intro.object]/1:

    An object is created by a definition, by a new-expression, when implicitly changing the active member of a union, or when a temporary object is created ([conv.rval], [class.temporary]). An object occupies a region of storage in its period of construction ([class.cdtor]), throughout its lifetime, and in its period of destruction ([class.cdtor]).

    The second sentence there answers your question. If an object is able to occupy a region of storage outside of the period described as "its lifetime" (like during its construction and destruction), then clearly an object must be able to exist outside of its lifetime.