<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2414: Member function reentrancy should be implementation-defined</title>
<meta property="og:title" content="Issue 2414: Member function reentrancy should be implementation-defined">
<meta property="og:description" content="C++ library issue. Status: Open">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2414.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Open">Open</a> status.</em></p>
<h3 id="2414"><a href="lwg-active.html#2414">2414</a>. Member function reentrancy should be implementation-defined</h3>
<p><b>Section:</b> 16.4.6.9 <a href="https://wg21.link/reentrancy">[reentrancy]</a> <b>Status:</b> <a href="lwg-active.html#Open">Open</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2014-07-01 <b>Last modified:</b> 2021-07-31</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#reentrancy">issues</a> in [reentrancy].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Open">Open</a> status.</p>
<p><b>Discussion:</b></p>
<p>
N3936 16.4.6.9 <a href="https://wg21.link/reentrancy">[reentrancy]</a>/1 talks about "functions", but that doesn't address the scenario of calling different member
functions of a single object. Member functions often have to violate and then re-establish invariants. For example, vectors
often have "holes" during insertion, and element constructors/destructors/etc. shouldn't be allowed to observe the vector
while it's in this invariant-violating state. The [reentrancy] Standardese should be extended to cover member functions,
so that implementers can either say that member function reentrancy is universally prohibited, or selectively allowed for
very specific scenarios.
<p/>
(For clarity, this issue has been split off from LWG <a href="lwg-closed.html#2382" title="Unclear order of container update versus object destruction on removing an object (Status: Pending NAD)">2382</a><sup><a href="https://cplusplus.github.io/LWG/issue2382" title="Latest snapshot">(i)</a></sup>.)
</p>

<p><i>[2014-11-03 Urbana]</i></p>

<p>
AJM confirmed with SG1 that they had no special concerns with this issue, and LWG should retain ownership.
<p/>
AM: this is too overly broad as it also covers calling the exact same member function on a different object<br/>
STL: so you insert into a map, and copying the value triggers another insertion into a different map of the same type<br/>
GR: reentrancy seems to imply the single-threaded case, but needs to consider the multi-threaded case
<p/>
Needs more wording.
</p>
<p>
Move to Open
</p>

<p><i>[2015-07 Telecon Urbana]</i></p>

<p>Marshall to ping STL for updated wording.<br/></p>

<p><i>[2016-05 email from STL]</i></p>

<p>I don't have any better suggestions than my original PR at the moment.</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">

<p>This wording is relative to N3936.</p>

<ol>
<li><p>Change 16.4.6.9 <a href="https://wg21.link/reentrancy">[reentrancy]</a> p1 as indicated:</p>
<blockquote>
<p>
-1- Except where explicitly specified in this standard, it is implementation-defined which functions <ins>(including different
member functions called on a single object)</ins> in the Standard C++ library may be recursively reentered.
</p>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2021-07-29 Tim suggests new wording]</i></p>

<p>
The "<code>this</code> pointer" restriction is modeled on 11.9.5 <a href="https://wg21.link/class.cdtor">[class.cdtor]</a> p2.
It allows us to continue to specify a member function <code>f</code> as calling some other
member function <code>g</code>, since any such call would use something obtained
from the first member function's <code>this</code> pointer.
<p/>
In all other cases, this wording disallows such "recursion on object" unless both
member functions are <code>const</code> (or are treated as such for the
purposes of data race avoidance). Using "access" means that we also cover direct
access to the object representation, such as the following pathological example
<a href="https://quuxplusone.github.io/blog/2021/04/17/pathological-string-appends/">from Arthur O'Dwyer</a>,
which is now undefined:
</p>
<blockquote>
<pre>
std::string s = "hello world";
char *first = (char*)&amp;s;
char *last = (char*)(&amp;s + 1);
s.append(first, last);
</pre>
</blockquote>


<p id="res-2414"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/N4892" title=" Working Draft, Standard for Programming Language C++">N4892</a>.</p>

<ol>
<li><p>Add the following paragraph to 16.4.6.9 <a href="https://wg21.link/reentrancy">[reentrancy]</a>:</p>
<blockquote>
<p>
<ins>-?- During the execution of a standard library non-static member function <i>F</i> on an object,
if that object is accessed through a glvalue that is not obtained, directly or indirectly, from
the <code>this</code> pointer of <i>F</i>, in a manner that can conflict (6.10.2.2 <a href="https://wg21.link/intro.races">[intro.races]</a>)
with any access that <i>F</i> is permitted to perform (16.4.6.10 <a href="https://wg21.link/res.on.data.races">[res.on.data.races]</a>),
the behavior is undefined unless otherwise specified.</ins>
</p>
</blockquote>
</li>
</ol>





</body>
</html>
