<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2564: [fund.ts.v2] std::experimental::function constructors taking allocator arguments may throw exceptions</title>
<meta property="og:title" content="Issue 2564: [fund.ts.v2] std::experimental::function constructors taking allocator arguments may throw exceptions">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2564.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#Resolved">Resolved</a> status.</em></p>
<h3 id="2564"><a href="lwg-defects.html#2564">2564</a>. [fund.ts.v2] <code>std::experimental::function</code> constructors taking allocator arguments may throw exceptions</h3>
<p><b>Section:</b> 4.2 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#func.wrap.func">[fund.ts.v2::func.wrap.func]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Tim Song  <b>Opened:</b> 2015-12-05 <b>Last modified:</b> 2022-11-22</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#fund.ts.v2::func.wrap.func">issues</a> in [fund.ts.v2::func.wrap.func].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p><b>Addresses: fund.ts.v2</b></p>
<p>
[This is essentially LWG <a href="lwg-defects.html#2370" title="Operations involving type-erased allocators should not be noexcept in std::function (Status: Resolved)">2370</a><sup><a href="https://cplusplus.github.io/LWG/issue2370" title="Latest snapshot">(i)</a></sup>, but deals with the fundamentals TS version rather than the one in the standard]
<p/>
In 4.2 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#func.wrap.func">[fund.ts.v2::func.wrap.func]</a> of library fundamentals TS, the constructors
</p>
<blockquote><pre>
template&lt;class A&gt; function(allocator_arg_t, const A&amp;) noexcept;
template&lt;class A&gt; function(allocator_arg_t, const A&amp;, nullptr_t) noexcept;
</pre></blockquote>
<p>
must type-erase and store the provided allocator, since the <code>operator=</code> specification requires using the "allocator 
specified in the construction of" the <code>std::experimental::function</code> object. This may require a dynamic allocation 
and so cannot be noexcept. Similarly, the following constructors
</p>
<blockquote><pre>
template&lt;class A&gt; function(allocator_arg_t, const A&amp;, const function&amp;); 
template&lt;class A&gt; function(allocator_arg_t, const A&amp;, function&amp;&amp;);
template&lt;class F, class A&gt; function(allocator_arg_t, const A&amp;, F);
</pre></blockquote>
<p>
cannot satisfy the C++14 requirement that they "shall not throw exceptions if [the <code>function</code> object to be stored] 
is a callable object passed via <code>reference_wrapper</code> or a function pointer" if they need to type-erase and store the 
allocator.
</p>
<p><i>[2016-11-08, Issaquah]</i></p>

<p>Not adopted during NB comment resolution</p>

<p><i>[2022-10-12 LWG telecon]</i></p>

<p>
Set status to "Open". This would be resolved by <a href="https://wg21.link/P0987R2" title=" polymorphic_allocator instead of type-erasure">P0987R2</a>.
</p>

<p><i>[2022-11-22 Resolved by <a href="https://wg21.link/P0897R2">P0897R2</a> accepted in Kona. Status changed: Open &rarr; Resolved.]</i></p>



<p id="res-2564"><b>Proposed resolution:</b></p>
<p>
This wording is relative to <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4562.html">N4562</a>.
</p>

<ol>
<li><p> Edit 4.2 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#func.wrap.func">[fund.ts.v2::func.wrap.func]</a>, class template <code>function</code> synopsis, as follows:</p>

<blockquote>
<pre>
namespace std {
  namespace experimental {
  inline namespace fundamentals_v2 {

    [&hellip;]

    template&lt;class R, class... ArgTypes&gt;
    class function&lt;R(ArgTypes...)> {
    public:    
      [&hellip;]
      template&lt;class A> function(allocator_arg_t, const A&amp;) <del>noexcept</del>;
      template&lt;class A> function(allocator_arg_t, const A&amp;,
        nullptr_t) <del>noexcept</del>;
      [&hellip;]
    };

    [&hellip;]

  } // namespace fundamentals_v2
  } // namespace experimental

  [&hellip;]

} // namespace std
</pre>
</blockquote>
</li>

<li><p>Insert the following paragraphs after 4.2.1 <a href="https://cplusplus.github.io/fundamentals-ts/v2.html#func.wrap.func.con">[fund.ts.v2::func.wrap.func.con]</a>/1:</p>
<blockquote class="note">
<p>
[<i>Drafting note</i>: This just reproduces the wording from C++14 with the "shall not throw exceptions for 
<code>reference_wrapper</code>/function pointer" provision deleted. &mdash; <i>end drafting note</i>]
</p>
</blockquote>

<blockquote>
<p>
-1- When a <code>function</code> constructor that takes a first argument of type <code>allocator_arg_t</code> is invoked, 
the second argument is treated as a <em>type-erased allocator</em> (8.3). If the constructor moves or makes a copy 
of a function object (C++14 &sect;20.9), including an instance of the <code>experimental::function</code> class template, 
then that move or copy is performed by <em>using-allocator construction</em> with allocator <code>get_memory_resource()</code>. 
</p>
<blockquote>
<pre>
<ins>template &lt;class A&gt; function(allocator_arg_t, const A&amp; a);
template &lt;class A&gt; function(allocator_arg_t, const A&amp; a, nullptr_t);</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Postconditions</i>: <code>!*this</code>.</ins>
</p>
</blockquote>
<pre>
<ins>template &lt;class A&gt; function(allocator_arg_t, const A&amp; a, const function&amp; f);</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Postconditions</i>: <code>!*this if !f</code>; otherwise, <code>*this</code> targets a copy of <code>f.target()</code>.</ins>
<p/>
<ins>-?- <i>Throws</i>: May throw <code>bad_alloc</code> or any exception thrown by the copy constructor of the stored callable object. 
[<i>Note</i>: Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, 
for example, where <code>f</code>'s target is an object holding only a pointer or reference to an object and a member function pointer. 
&mdash; <i>end note</i>]</ins>
</p>
</blockquote>
<pre>
<ins>template &lt;class A&gt; function(allocator_arg_t, const A&amp; a, function&amp;&amp; f);</ins>
</pre>
<blockquote>
<p>
<ins>-?- Effects: If <code>!f</code>, <code>*this</code> has no target; otherwise, move-constructs the target of <code>f</code> into the 
target of <code>*this</code>, leaving <code>f</code> in a valid state with an unspecified value.</ins>
</p>
</blockquote>
<pre>
<ins>template &lt;class F, class A&gt; function(allocator_arg_t, const A&amp; a, F f);</ins>
</pre>
<blockquote>
<p>
<ins>-?- <i>Requires</i>: <code>F</code> shall be <code>CopyConstructible</code>.</ins>
<p/>
<ins>-?- <i>Remarks</i>: This constructor shall not participate in overload resolution unless <code>f</code> is Callable 
(C++14 &sect;20.9.11.2) for argument types <code>ArgTypes...</code> and return type <code>R</code>.</ins>
<p/>
<ins>-?- <i>Postconditions</i>: <code>!*this</code> if any of the following hold:</ins>
<ul>
<li><p><ins><code>f</code> is a null function pointer value.</ins></p></li>
<li><p><ins><code>f</code> is a null member pointer value.</ins></p></li>
<li><p><ins><code>F</code> is an instance of the <code>function</code> class template, and <code>!f</code>.</ins></p></li>
</ul>
<p/>
<ins>-?- Otherwise, <code>*this</code> targets a copy of <code>f</code> initialized with <code>std::move(f)</code>. [<i>Note</i>: 
Implementations are encouraged to avoid the use of dynamically allocated memory for small callable objects, for example, 
where <code>f</code>'s target is an object holding only a pointer or reference to an object and a member function pointer. &mdash; 
<i>end note</i>]</ins>
<p/>
<ins>-?- <i>Throws</i>: May throw <code>bad_alloc</code> or any exception thrown by <code>F</code>'s copy or move constructor.</ins>
</p>
</blockquote>
</blockquote>
<p>
-2- In the following descriptions, let <code><i>ALLOCATOR_OF</i>(f)</code> be the allocator specified in the construction 
of <code>function f</code>, or <code>allocator&lt;char&gt;()</code> if no allocator was specified.
<p/>
[&hellip;]
</p>
</blockquote>
</li>
</ol>





</body>
</html>
