<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Core Issue 1776: Replacement of class objects containing reference members</title>

<style type="text/css">
  ins, ins * { text-decoration:none; font-weight:bold; background-color:#A0FFA0 }
  del, del * { text-decoration:line-through; background-color:#FFA0A0 }
  #hidedel:checked ~ * del, #hidedel:checked ~ * del * { display:none; visibility:hidden }

blockquote { color: #000000; background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding-left: 0.5em; padding-right: 0.5em; }
blockquote.stdins { text-decoration: underline;
  color: #000000; background-color: #C8FFC8;
  border: 1px solid #B3EBB3; padding: 0.5em; }
blockquote.stddel { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding-left: 0.5empadding-right: 0.5em; ; }
</style>

</head>

<body>
<div style="text-align: right; float: right">
ISO/IEC JTC1 SC22 WG21<br>
N4430<br>
Richard Smith<br>
richard@metafoo.co.uk<br>
2015-04-10<br>
</div>

<h1>Core Issue 1776: Replacement of class objects containing reference members</h1>

<!--

TODO:

add notes from hubert's 6 Mar email

-->

<input type="checkbox" id="hidedel">Hide deleted text</input>

<p>
Change in 1.8 (intro.object) paragraph 1:
</p>

<blockquote>
The constructs in a C++
program create, destroy, refer to, access, and manipulate objects.
<ins>
An <em>object</em> is created by a <em>definition</em> (3.1), by a <em>new-expression</em> (5.3.4)
or by the implementation (12.2) when needed.
</ins>
An <del><em>object</em> is</del><ins>object occupies</ins> a region of storage <ins>throughout its lifetime (3.8 basic.life)</ins>.
[&nbsp;<em>Note</em>:
A function is not an object, regardless of whether or not it occupies storage in the
way that objects do.
&mdash;&nbsp;<em>end note</em>&nbsp;]
<del>
An object is created by a <em>definition</em> (3.1), by a <em>new-expression</em> (5.3.4)
or by the implementation (12.2) when needed.
</del>
[&hellip;]
</blockquote>

<em>Drafting note: this maintains the status quo that <tt>malloc</tt> alone is
  not sufficient to create an object.</em>

<p>
Change in 1.8 (intro.object) paragraph 6:
</p>

<blockquote>
Unless an object is a bit-field or a base class subobject of zero size,
the address of that object is the address of the first byte it occupies.
Two objects <ins>with overlapping lifetimes</ins> that are not bit-fields
may have the same address if one is a subobject of the other,
or if at least one is a base class subobject of zero size and they are of different types;
otherwise, they shall have distinct addresses. [Footnote: &hellip;] [Example: &hellip;]
</blockquote>

<p>
Add a new paragraph after 3.7 (basic.stc) paragraph 3:
</p>

<blockquote class="stdins">
When the end of the duration of a region of storage is reached,
the values of all pointers representing the address of
any part of the deallocated storage
become invalid pointer values ([basic.compound]).
Indirection through
an invalid pointer value and passing an invalid pointer value to a deallocation
function have undefined behavior. Any other use of an invalid pointer value has
implementation-defined behavior.
[&nbsp;<em>Footnote</em>:
Some implementations might define that copying an invalid pointer value causes
a system-generated runtime fault.
&mdash;&nbsp;<em>end footnote</em>&nbsp;]
</blockquote>

<em>Drafting note: this should apply to all storage durations that can end,
  not just to dynamic storage duration. On an implementation supporting threads
  or segmented stacks, thread and automatic storage may behave in the same
  way that dynamic storage does.</em>

<p>
Change in 3.7.4.1 (basic.stc.dynamic.allocation) paragraph 2:
</p>

<blockquote>
[&hellip;]
Furthermore, for the library allocation functions in 18.6.1.1 and
18.6.1.2, p0 shall <del>point to</del> <ins>represent the address of</ins>
a block of storage disjoint from the storage for
any other object accessible to the caller. The effect of indirecting through a
pointer returned as a request for zero size is undefined.
</blockquote>

<p>
Change in 3.7.4.1 (basic.stc.dynamic.allocation) paragraph 2:
</p>

<blockquote>
If the argument given to a deallocation function in the standard library is a
pointer that is not the null pointer value (4.10), the deallocation function
shall deallocate the storage referenced by the pointer,
<ins>ending the duration of the region of storage.<ins>
<del>
rendering invalid all
pointers referring to any part of the deallocated storage. Indirection through
an invalid pointer value and passing an invalid pointer value to a deallocation
function have undefined behavior. Any other use of an invalid pointer value has
implementation-defined behavior.
[&nbsp;<em>Footnote</em>:
Some implementations might define that copying an invalid pointer value causes
a system-generated runtime fault.
&mdash;&nbsp;<em>end footnote</em>&nbsp;]
</del>
</blockquote>

<p>
Change in 3.8 (basic.life) paragraph 5:
</p>

<blockquote>
Before the lifetime of an object has started but after the storage which the object will occupy has been allocated [Footnote: &hellip;] or,
after the lifetime of an object has ended and before the storage which the object occupied is reused or released,
any pointer that <del>refers to</del> <ins>represents the address of</ins> the storage location
where the object will be or was located may be used but only in limited ways.
[&hellip;]
</blockquote>

<p>
Change in 3.8 (basic.life) paragraph 9:
</p>

<blockquote>
Creating a new object at the storage location that a <tt>const</tt> <ins>complete</ins> object with static<del>,</del> <ins>or</ins> thread<del>, or automatic</del> storage
duration occupies or<del>,</del> at the storage location that such a <tt>const</tt> object used to occupy before its lifetime
ended results in undefined behavior. [Example: &hellip;]
</blockquote>

<em>This is necessary to allow types such as <tt>std::optional</tt> to contain const subobjects; the existing restriction exists to allow ROMability, and so only affects complete objects and is not necessary for automatic storage duration objects.</em>

<p>
Change in 3.9.2 (basic.compound) paragraph 3:
</p>

<blockquote>
[&hellip;]
<ins>A value of an object pointer type <i>cv</i> <tt>T*</tt> either:
<ul><li><em>points to</em> an object or function, or
    <li><em>points past the end of</em> an object (5.3.1 expr.unary.op), or
    <li>is the <em>null pointer value</em> (4.10 conv.ptr) for that type, or
    <li>is an <em>invalid pointer value</em>.
</ul>
</ins>
A <del>valid</del> value of a<del>n object</del> pointer type <ins>that points to or past the end of an object</ins> <del>represents either
the address</del> <ins><em>represents the address</em></ins>
of <del>a</del> <ins>the first</ins> byte in memory (1.7 intro.memory)
<ins>that the object occupies</ins>
<del>or a null pointer (4.10)</del>.
<del>
If an object of type T is located at an address A, a pointer of type cv T* whose value is the
address A is said to point to that object, regardless of how the value was obtained.
</del>
[&nbsp;<em>Note</em>:
<del>For instance, the address one</del> <ins>A pointer</ins> past the
end of an <del>array</del> <ins>object</ins> (5.7)
<del>would be</del>
<ins>is not</ins>
considered to point to an unrelated object of the
<del>array's element</del> <ins>object's</ins> type
that might be located at that address.
<del>
There are further restrictions on pointers to
objects with dynamic storage duration; see 3.7.4.3.
</del>
<ins>
A pointer acquires an invalid pointer value
when the storage it denotes
reaches the end of its storage duration; see [basic.stc].
</ins>
&mdash;&nbsp;<em>end note</em>&nbsp;]
<ins>For purposes of pointer arithmetic (5.7 expr.add) and comparison (5.9 expr.rel, 5.10 expr.eq),
a pointer that points past the end of the last element of an array of <i>n</i> elements is considered
to point to a hypothetical element <i>n</i>.</ins>
The value representation of pointer types is implementation-defined.
Pointers to cv-qualified and cv-unqualified versions (3.9.3)
of layout-compatible types
shall have the same value representation and alignment requirements (3.11).
[&nbsp;<em>Note</em>: &hellip; &mdash;&nbsp;<em>end note</em>&nbsp;]
</blockquote>

<p>
Change in 5.7 (expr.add) paragraph 4:
</p>

<blockquote>
When an expression that has integral type is added to or subtracted from a pointer,
the result has the type of the pointer operand.

<del>
If the pointer operand points to an element of an array object [Footnote],
and the array is large enough,
the result points to an element offset from the original element
such that the difference of the subscripts
of the resulting and original array elements
equals the integral expression.

In other words, if
</del><ins>If</ins>
the expression <tt>P</tt>
points to <del>the <em>i</em>-th</del> element <ins><i>i</i></ins>
of an array object <ins>with <em>n</em> elements
[&nbsp;<i>Footnote</i>:
An object that is not an array element is considered to belong to a single-element array for this purpose; see 5.3.1.
A pointer past the end of the last element of an array of <i>n</i> elements
is considered to point to a hypothetical element <i>n</i> for this purpose; see 3.9.2.
]</ins>,
the expressions

<del><tt>(P)+N</tt> (equivalently, <tt>N+(P)</tt>) and <tt>(P)-N</tt></del>

<ins><tt>P + <ins>J</ins></tt> and <tt>J + P</tt></ins>

(where <tt><del>N</del><ins>J</ins></tt> has the value <i><del>n</del><ins>j</ins></i>)
point to<del>, respectively, the</del>
<ins>element</ins> <em>i + j</em><del>-th and <em>i - n</em>-th elements</del>
of the array object
<del>, provided they exist</del>
<ins>if 0 &le; <em>i + j</em> &lt; <em>n</em>,
and point past the last element of the array object if <em>i + j</em> = <em>n</em></ins><del>
Moreover, if the expression P points to the last element of an array object,
the expression (P)+1 points one past the last element of the array object, and if the expression Q points
one past the last element of an array object, the expression (Q)-1 points to the last element of the array
object. If both the pointer operand and the result point to elements of the same array object, or one past
the last element of the array object, the evaluation shall not produce an overflow</del>; otherwise, the behavior is
undefined.

<ins>Likewise, the expression <tt>P - J</tt> points to element <i>i - j</i> of the array object
if 0 &le; <em>i - j</em> &lt; <em>n</em>,
and point past the last element of the array object if <em>i - j</em> = <em>n</em>; otherwise, the behavior is undefined.
</ins>
</blockquote>

<p>
Change in 5.7 (expr.add) paragraph 5:
</p>

<blockquote>
When two pointers to elements of the same array object are subtracted,
<del>
the result is the difference of the subscripts of the two array elements.
The</del> <ins>the</ins>
type of the result is an implementation-defined signed integral type;
this type shall be the same type that is defined as <tt>std::ptrdiff_t</tt> in the <tt>&lt;cstddef></tt> header (18.2).
<del>
As with any other arithmetic overflow, if the result does not fit in the space provided, the behavior is undefined.
In other words, if
</del> <ins>If</ins>
the expressions <tt>P</tt> and <tt>Q</tt> point to, respectively,
<del>the i-th and j-th</del> elements <ins><i>i</i> and <i>j</i></ins> of <del>an</del><ins>the same</ins> array object,
the expression <del>(P)-(Q)</del><ins>P - Q</ins> has the value <i>i - j</i>
<del>provided the value fits in an object of type <tt>std::ptrdiff_t</tt>.</del>
<ins>; otherwise, the behavior is undefined.
[&nbsp;<em>Note</em>:
If the value <i>i - j</i> is not in the range of representable values of type <tt>std::ptrdiff_t</tt>, the behavior is undefined.
&mdash;&nbsp;<em>end note</em>&nbsp;]</ins>
<del>
Moreover, if the expression P points either to an element of an array object or one past the last element of
an array object, and the expression Q points to the last element of the same array object, the expression
((Q)+1)-(P) has the same value as ((Q)-(P))+1 and as -((P)-((Q)+1)), and has the value zero if the
expression P points one past the last element of the array object, even though the expression (Q)+1 does not
point to an element of the array object. Unless both pointers point to elements of the same array object, or
one past the last element of the array object, the behavior is undefined.
[Footnote: Another way to approach pointer arithmetic &hellip;]
</del>
</blockquote>

<p>
Change in 5.7 (expr.add) paragraph 7:
</p>

<blockquote>
If the value 0 is added to or subtracted from a <ins>null</ins> pointer value, the result <del>compares equal to the original pointer
value</del> <ins>is a null pointer value</ins>.
If two <del>pointers point to the same object or both point  past the end of the same array or both
are</del> null<del>, and the two</del> pointer<ins> value</ins>s are subtracted, the result compares equal to the value 0 converted to the type
<tt>std::ptrdiff_t.</tt>
</blockquote>

<p>
Change in 5.9 (expr.rel) paragraph 3:
</p>

<blockquote>
Comparing <ins>unequal</ins> pointers to objects
[&nbsp;<i>Footnote</i>:
An object that is not an array element is considered to belong to a single-element array for this purpose; see 5.3.1.
<ins>
A pointer past the end of the last element of an array of <i>n</i> elements
is considered to point to a hypothetical element <i>n</i> for this purpose; see 3.9.2.
</ins>
]
is defined as follows:
<ul>
<li>If two pointers point to different elements of the same array, or to subobjects thereof, the pointer to
the element with the higher subscript compares greater.
<li><del>If one pointer points to an element of an array, or to a subobject thereof, and another pointer points
one past the last element of the array, the latter pointer compares greater.</del>
<li>
If two pointers point to different non-static data members of the same object, or to subobjects of such
members, recursively, the pointer to the later declared member compares greater provided the two
members have the same access control (Clause 11) and provided their class is not a union.
</ul>
</blockquote>

<em>Drafting note: the change in 3.9.2 affects the semantics of an example like:</em>

  <pre>
  struct S {
    char a;
    int b;
  } s;
  static_assert(static_cast&lt;void*>(&s.b) >= static_cast&lt;void*>(&s.a + 1));</pre>

<em>Prior to this change, the result of the comparison was unspecified; now, it is required to hold, because <tt>&s.a + 1</tt> is treated as a pointer to an element of the "array" <tt>s.a</tt>. The value of <tt>&s.b > &s.a + 1</tt> is now specified and is <tt>false</tt> if and only if the pointers represent the same address.</em>

<p>
Change in 9.2 class.mem paragraph 19:
</p>

<blockquote>
If a standard-layout class object
has any non-static data members,
its 
address is the same as the address of
its first non-static data member.
Otherwise,
its
address is the same as the address of
its first base class subobject (if any).
[&nbsp;<em>Note</em>&nbsp;&hellip;&mdash;<em>end note</em>&nbsp;]
<ins>
[&nbsp;<em>Note</em>&nbsp;
Nonetheless, if a pointer to the object
is cast to a pointer to
the type of its first subobject (or vice versa),
the resulting pointer does not point to the
subobject and can not be used to access it.
&mdash;<em>end note</em>&nbsp;]
</ins>
</blockquote>

<p>
Change in 9.5 class.union paragraph 1, splitting it into two paragraphs:
</p>

<blockquote>
<p>
In a union,
<ins>a non-static data member is <em>active</em>
if its name refers to an object
whose lifetime has begun and has not ended ([basic.life]).</ins>
<del>at</del><ins>At</ins> most one of the non-static data members
<ins>of an object of union type</ins>
can be active at any
time, that is, the value of at most one of the non-static data members can be
stored in a union at any time.
[&nbsp;<em>Note</em>:
One special guarantee is made in order
to simplify the use of unions:
If a standard-layout union contains several
standard-layout structs that share a common initial sequence (9.2),
and if <ins>the active member of</ins> an object
of this standard-layout union type
<del>contains</del> <ins>is</ins> one of the standard-layout structs,
it is permitted to inspect the common initial sequence of any of <ins>the</ins>
standard-layout struct members; see 9.2.
&mdash;&nbsp;<em>end note</em>&nbsp;]

<p>
The size of a union is sufficient to contain the largest of its non-static data members.
Each non-static data member is allocated as if it were the sole member of a struct.
All non-static data members of a union object have the same address.
<ins>
[&nbsp;<em>Note</em>&nbsp;
Nonetheless, if a pointer to a union object
is cast to a pointer to
the type of a non-static data member (or vice versa),
the resulting pointer does not point to the
subobject and can not be used to access it.
&mdash;<em>end note</em>&nbsp;]
</ins>
</blockquote>

<p>
Add to 18.6 (support.dynamic) paragraph 1:
</p>

<blockquote class="stdins"><pre>
namespace std {
  template &lt;class T>
    constexpr T* launder(T* p) noexcept;
}
</pre></blockquote>

<p>
Add a new subclause under 18.6 (support.dynamic):
</p>

<blockquote class="stdins">
  <pre>template &lt;class T> constexpr T* launder(T* p) noexcept;</pre>

<p><em>Requires:</em> <tt>p</tt> represents the address <tt>A</tt> of a byte in memory, and an object <tt>X</tt> that is within its lifetime and whose cv-unqualified type is similar to <tt>T</tt> is located at the address <tt>A</tt>.
<p><em>Returns:</em> A value of type <tt>T *</tt> that points to <tt>X</tt>.
<p><em>Note:</em> An invocation of this function may be used in a core constant expression whenever the value of its argument may be used in a core constant expression.
<p>
[&nbsp;<em>Example</em>:
<pre>
struct X { const int n; };
X *p = new X{3};
const int a = p->n;
new (p) X{5};                     // p does not point to new object ([basic.life])
                                  // because X::n is const
const int b = p->n;               // undefined behavior
const int c = std::launder(p)->n; // ok
</pre>
&mdash;&nbsp;<em>end example</em>&nbsp;]
</blockquote>

<em>Drafting note: see N4303 for more background on the purpose and intended usage of this function.</em>

<p>
Change in 20.7.5 (ptr.align) paragraph 1:
</p>

<blockquote>
<i>Effects:</i>
If it is possible to fit <tt>size</tt> bytes of storage aligned by <tt>alignment</tt> into the buffer pointed to by
<tt>ptr</tt> with length <tt>space</tt>, the function updates <tt>ptr</tt> to
<del>point to</del> <ins>represent</ins> the first possible
address of such storage and decreases <tt>space</tt> by the number of bytes
used for alignment.
</blockquote>

<p>
Change in 20.7.5 (ptr.align) paragraph 2:
</p>

<blockquote>
<i>Requires:</i>
<ul><li><tt>alignment</tt> shall be a power of two
    <li><tt>ptr</tt> shall <del>point to</del><ins>represent the address of</ins> contiguous storage of at least <tt>space</tt> bytes
</ul>
</blockquote>
 
</body></html>
