Issue 159259
Summary Backporting implicit object creation rules from c++20 to older standards
Labels new issue
Assignees
Reporter safocl
    C++20 introduces additions to [intro.object], defining object creation as "_operations that implicitly create objects_" (intro.object#1, intro.object#11, intro.object#12, intro.object#13, intro.object#14) and other relevant sections.
Did the Clang++ compiler backport these additions to earlier standards?
in particular, will the example from C++20 [intro.object#13] work without undefined behavior?
```cpp
#include <cstdlib>
struct X { int a, b; };
X *make_x() {
  // The call to std​::​malloc implicitly creates an object of type X
  // and its subobjects a and b, and returns a pointer to that X object
  // (or an object that is pointer-interconvertible ([[basic.compound]](https://eel.is/c++draft/basic.compound)) with it),
  // in order to give the subsequent class member access operations
  // defined behavior.
  X *p = (X*)std::malloc(sizeof(struct X));
  p->a = 1;
  p->b = 2;
  return p;
}
```

C++20 additions:
[[intro.object#1]](https://eel.is/c++draft/intro.object#1.sentence-2)

> An [object](https://eel.is/c++draft/intro.object#def:object) is created [...] , by an operation that implicitly creates objects (see below)

[[intro.object#11]](https://eel.is/c++draft/intro.object#11)

> [11](https://eel.is/c++draft/intro.object#11) Some operations are described as [implicitly creating objects](https://eel.is/c++draft/intro.object#def:object,implicit_creation) within a specified region of storage[.](https://eel.is/c++draft/intro.object#11.sentence-1) 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]](https://eel.is/c++draft/basic.types.general#term.implicit.lifetime.type)) in its specified region of storage if doing so would result in the program having defined behavior[.](https://eel.is/c++draft/intro.object#11.sentence-2) If no such set of objects would give the program defined behavior, the behavior of the program is undefined[.](https://eel.is/c++draft/intro.object#11.sentence-3) If multiple such sets of objects would give the program defined behavior, it is unspecified which such set of objects is created[.](https://eel.is/c++draft/intro.object#11.sentence-4)
> [Note [4](https://eel.is/c++draft/intro.object#note-4): Such operations do not start the lifetimes of subobjects of such objects that are not themselves of implicit-lifetime types[.](https://eel.is/c++draft/intro.object#11.sentence-5) — end note]

[[intro.object#12]](https://eel.is/c++draft/intro.object#12)

> [12](https://eel.is/c++draft/intro.object#12) Further, after implicitly creating objects within a specified region of storage, some operations are described as producing a pointer to a [suitable created object](https://eel.is/c++draft/intro.object#def:object,suitable_created)[.](https://eel.is/c++draft/intro.object#12.sentence-1) These operations select one of the implicitly-created objects whose address is the address of the start of the region of storage, and produce a pointer value that points to that object, if that value would result in the program having defined behavior[.](https://eel.is/c++draft/intro.object#12.sentence-2) If no such pointer value would give the program defined behavior, the behavior of the program is undefined[.](https://eel.is/c++draft/intro.object#12.sentence-3) If multiple such pointer values would give the program defined behavior, it is unspecified which such pointer value is produced[.](https://eel.is/c++draft/intro.object#12.sentence-4)

[[intro.object#14]](https://eel.is/c++draft/intro.object#14)

> [14](https://eel.is/c++draft/intro.object#14) Except during constant evaluation, an operation that begins the lifetime of an array of unsigned char or std​::​byte implicitly creates objects within the region of storage occupied by the array[.](https://eel.is/c++draft/intro.object#14.sentence-1)
> [Note [5](https://eel.is/c++draft/intro.object#note-5): The array object provides storage for these objects[.](https://eel.is/c++draft/intro.object#14.sentence-2) — end note]
> Except during constant evaluation, any implicit or explicit invocation of a function named operator new or operator new[] implicitly creates objects in the returned region of storage and returns a pointer to a suitable created object[.](https://eel.is/c++draft/intro.object#14.sentence-3)
> [Note [6](https://eel.is/c++draft/intro.object#note-6): Some functions in the C++ standard library implicitly create objects ([[obj.lifetime]](https://eel.is/c++draft/obj.lifetime), [[c.malloc]](https://eel.is/c++draft/c.malloc), [[mem.res.public]](https://eel.is/c++draft/mem.res.public), [[bit.cast]](https://eel.is/c++draft/bit.cast), [[cstring.syn]](https://eel.is/c++draft/cstring.syn))[.](https://eel.is/c++draft/intro.object#14.sentence-4) — end note]
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to