C++ Logo


Advanced search

Subject: Re: Implicit object creation, arrays, and non-implicit lifetime objects
From: Jason McKesson (jmckesson_at_[hidden])
Date: 2021-03-25 21:59:05

On Thu, Mar 25, 2021 at 10:28 PM <language.lawyer_at_[hidden]> wrote:
> On 26/03/2021 05:18, Jason McKesson via Std-Discussion wrote:
> > But what does `malloc` return?
> >
> > According to the standard, it returns a "pointer to a suitable created
> > object". But the only object that was created was a `T[10]`, not the
> > `T`s themselves.
> https://timsong-cpp.github.io/cppwp/n4861/intro.object#12.example-1 says:
> > struct X { int a, b; };
> > // The call to std::malloc implicitly creates an object of type X
> > // and its subobjects a and b
> I think the same may apply to the `T[10]` case: `T[10]` and its elements are created (but only the lifetime of `T[10]` is started). So malloc can return a pointer to `T[0]`.
> But I'm not sure this is 100% true.

That example doesn't really apply, because:

1. The code actually uses the subobjects, therefore they *must* have
been part of IOC, and

2. The types of those members are implicit lifetime types (ILTs) and
therefore can be the subject of IOC.

Now, there is a notation that "Such operations do not start the
lifetimes of subobjects of such objects that are not themselves of
implicit-lifetime types." That would support your interpretation, but
this is non-normative text. I don't see anything in the actual text to
support this interpretation.

If this were the case, I would expect to see some statement of how IOC
works on arrays of non-ILTs (which is the only way an ILT can have
subobjects that aren't ILTs). But there is no statement in the
lifetime section that, when an array of non-ILT is created implicitly,
the subobjects are created but their lifetimes are not started.

Again, the behavior of `std::allocator::allocate` explicitly says that
the array's lifetime starts but not that of its elements. I don't see
that statement generally being made about IOC-style arrays (outside of
the note). Indeed, there are several places that all say similar
things (the `a.allocate` table entry of the allocation requirements,
which BTW doesn't say what the return value actually is). So we could
simplify this repetition by making a blanket statement about IOC-array
creation, and simply referencing it. `allocate` would therefore create
an array of T in the storage as if through IOC array creation.

STD-DISCUSSION list run by std-discussion-owner@lists.isocpp.org

Older Archives on Google Groups