Consider p0593 and the following code:
struct A { std::string s; };
static_assert(std::is_implicit_lifetime_v<A>); // true!
// std::malloc is explicitly BLESSED by the C++23 standard.
// But my_alloc is not.
void* my_alloc() {
return std::malloc(sizeof(A));
}
int main() {
auto p1 = static_cast<A*>(std::malloc(sizeof(A)));
// OK. An object of A is implicitly created and begins its lifetime.
auto p2 = static_cast<A*>(my_alloc());
// OK? Is there an object of A implicitly created here?
}
Does the IOC (Implicitly Object Creation) apply to my_alloc wrapping std::malloc
?
From https://eel.is/c++draft/intro.object#11
Some operations are described as implicitly creating objects within a specified region of storage. For each operation that is specified as implicitly creating objects, that operation implicitly creates and starts the lifetime of zero or more objects of implicit-lifetime types ([basic.types.general]) in its specified region of storage if doing so would result in the program having defined behavior. If no such set of objects would give the program defined behavior, the behavior of the program is undefined. If multiple such sets of objects would give the program defined behavior, it is unspecified which such set of objects is created.
IOC starts with std::malloc
with any valid type to make the program correct (if possible).
It is not the static_cast<A*>
which starts the creation, but the static_cast
reduces the set of possible types.
So transitively, we can consider that my_alloc
does the "same".