From Jason Turner

[ptr.launder]

Diff to HTML by rtfpessoa

Files changed (1) hide show
  1. tmp/tmps157_gvg/{from.md → to.md} +19 -17
tmp/tmps157_gvg/{from.md → to.md} RENAMED
@@ -1,41 +1,43 @@
1
  ### Pointer optimization barrier <a id="ptr.launder">[[ptr.launder]]</a>
2
 
3
  ``` cpp
4
- template <class T> constexpr T* launder(T* p) noexcept;
5
  ```
6
 
7
- *Requires:* `p` represents the address *A* of a byte in memory. An
8
- object *X* that is within its lifetime ([[basic.life]]) and whose type
9
- is similar ([[conv.qual]]) to `T` is located at the address *A*. All
10
- bytes of storage that would be reachable through the result are
11
- reachable through `p` (see below).
 
 
12
 
13
  *Returns:* A value of type `T*` that points to `X`.
14
 
15
  *Remarks:* An invocation of this function may be used in a core constant
16
  expression whenever the value of its argument may be used in a core
17
- constant expression. A byte of storage is reachable through a pointer
18
- value that points to an object *Y* if it is within the storage occupied
19
- by *Y*, an object that is pointer-interconvertible with *Y*, or the
20
- immediately-enclosing array object if *Y* is an array element. The
21
- program is ill-formed if `T` is a function type or cv `void`.
22
 
23
  [*Note 1*: If a new object is created in storage occupied by an
24
  existing object of the same type, a pointer to the original object can
25
- be used to refer to the new object unless the type contains `const` or
26
- reference members; in the latter cases, this function can be used to
27
- obtain a usable pointer to the new object.
28
  See  [[basic.life]]. — *end note*]
29
 
30
  [*Example 1*:
31
 
32
  ``` cpp
33
- struct X { const int n; };
34
- X *p = new X{3};
35
  const int a = p->n;
36
- new (p) X{5}; // p does not point to new object ([basic.life]) because X::n is const
37
  const int b = p->n; // undefined behavior
38
  const int c = std::launder(p)->n; // OK
39
  ```
40
 
41
  — *end example*]
 
1
  ### Pointer optimization barrier <a id="ptr.launder">[[ptr.launder]]</a>
2
 
3
  ``` cpp
4
+ template<class T> [[nodiscard]] constexpr T* launder(T* p) noexcept;
5
  ```
6
 
7
+ *Mandates:* `!is_function_v<T> && !is_void_v<T>` is `true`.
8
+
9
+ *Preconditions:* `p` represents the address *A* of a byte in memory. An
10
+ object *X* that is within its lifetime [[basic.life]] and whose type is
11
+ similar [[conv.qual]] to `T` is located at the address *A*. All bytes of
12
+ storage that would be reachable through the result are reachable through
13
+ `p` (see below).
14
 
15
  *Returns:* A value of type `T*` that points to `X`.
16
 
17
  *Remarks:* An invocation of this function may be used in a core constant
18
  expression whenever the value of its argument may be used in a core
19
+ constant expression. A byte of storage *b* is reachable through a
20
+ pointer value that points to an object *Y* if there is an object *Z*,
21
+ pointer-interconvertible with *Y*, such that *b* is within the storage
22
+ occupied by *Z*, or the immediately-enclosing array object if *Z* is an
23
+ array element.
24
 
25
  [*Note 1*: If a new object is created in storage occupied by an
26
  existing object of the same type, a pointer to the original object can
27
+ be used to refer to the new object unless its complete object is a const
28
+ object or it is a base class subobject; in the latter cases, this
29
+ function can be used to obtain a usable pointer to the new object.
30
  See  [[basic.life]]. — *end note*]
31
 
32
  [*Example 1*:
33
 
34
  ``` cpp
35
+ struct X { int n; };
36
+ const X *p = new const X{3};
37
  const int a = p->n;
38
+ new (const_cast<X*>(p)) const X{5}; // p does not point to new object [basic.life] because its type is const
39
  const int b = p->n; // undefined behavior
40
  const int c = std::launder(p)->n; // OK
41
  ```
42
 
43
  — *end example*]