<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2180: Exceptions from std::seed_seq operations</title>
<meta property="og:title" content="Issue 2180: Exceptions from std::seed_seq operations">
<meta property="og:description" content="C++ library issue. Status: C++14">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2180.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#C++14">C++14</a> status.</em></p>
<h3 id="2180"><a href="lwg-defects.html#2180">2180</a>. Exceptions from <code>std::seed_seq</code> operations</h3>
<p><b>Section:</b> 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> <b>Status:</b> <a href="lwg-active.html#C++14">C++14</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2012-08-18 <b>Last modified:</b> 2016-01-28</p>
<p><b>Priority: </b>Not Prioritized
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.util.seedseq">issues</a> in [rand.util.seedseq].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++14">C++14</a> status.</p>
<p><b>Discussion:</b></p>

<p>
29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> p1 says upfront:
</p>
<blockquote><p>
No function described in this section 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> throws an exception.
</p></blockquote>
<p>
This constraint seems non-implementable to me when looking especially at the members
</p>
<blockquote><pre>
template&lt;class T&gt;
seed_seq(initializer_list&lt;T&gt; il);

template&lt;class InputIterator&gt;
seed_seq(InputIterator begin, InputIterator end);
</pre></blockquote>
<p>
which have the effect of invoking <code>v.push_back()</code> for the exposition-only member of
type <code>std::vector</code> (or its equivalent) over all elements of the provided range, so
out-of-memory exceptions are always possible and the <code>seed_seq</code> object doesn't seem
to be constructible this way.
<p/>
In addition to the potential lack-of-resources problem, the operations of <code>InputIterator</code>
might also throw exceptions.
<p/>
Aside to that it should me mentioned, that a default constructor of <code>vector&lt;uint_least32_t&gt;</code> 
in theory can also throw exceptions, even though this seems less of a problem to me in this context, because 
such an implementation could easily use a different internal container in <code>seed_seq</code> that can hold 
this no-throw exception guarantee.
<p/>
Secondly, a slightly different problem category related to exceptions occurs for the member templates
</p>
<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
void generate(RandomAccessIterator begin, RandomAccessIterator end);

template&lt;class OutputIterator&gt;
void param(OutputIterator dest) const;
</pre></blockquote>
<p>
where the actual operations performed by the implementation would never need to throw, but since they invoke
operations of a user-provided customization point, the overall operation, like for example
</p>
<blockquote><pre>
copy(v.begin(), v.end(), dest);
</pre></blockquote>
<p>
could also throw exceptions. In this particular example we can just think of a <code>std::back_insert_iterator</code>
applied to a container that needs to allocate its elements used as the type for <code>OutputIterator</code>.
<p/>
Even though Clause 29 <a href="https://wg21.link/numerics">[numerics]</a> has mostly stronger exception constraints than other parts of the
library the here discussed are overrestrictive, especially since no operation of <code>std::seed_seq</code> 
except the template <code>generate</code> is actually needed within the library implementation, as mentioned in the 
discussion of LWG <a href="lwg-closed.html#2124" title="Seed sequence over-specified (Status: NAD)">2124</a><sup><a href="https://cplusplus.github.io/LWG/issue2124" title="Latest snapshot">(i)</a></sup>.
<p/>
I suggest to remove the general no-exception constraints for operations of <code>std::seed_seq</code> except for
member <code>size()</code> and the default constructor and to provide specific wording for <code>generate()</code> and
<code>param()</code> to ensure that the algorithm itself is a nothrow operation, which is especially for
<code>generate()</code> important, because the templates specified in 29.5.4 <a href="https://wg21.link/rand.eng">[rand.eng]</a> and 
29.5.5 <a href="https://wg21.link/rand.adapt">[rand.adapt]</a> also depend on this property indirectly, which is further discussed in LWG 
<a href="lwg-defects.html#2181" title="Exceptions from seed sequence operations (Status: C++17)">2181</a><sup><a href="https://cplusplus.github.io/LWG/issue2181" title="Latest snapshot">(i)</a></sup>.
<p/>
<u>Howard</u>:
<p/>
I suggest to use a different form for the exception specification, something similar to 
22.10.15.4 <a href="https://wg21.link/func.bind.bind">[func.bind.bind]</a> p4:
</p>
<blockquote><p>
<i>Throws</i>: Nothing unless an operation on <code>RandomAccessIterator</code> throws an exception.
</p></blockquote>
<p>
<u>Daniel</u>:
<p/>
The currently suggested "what and when" form seems a bit more specific and harmonizes with the form used for
function template <code>generate_canonical</code> from 29.5.8.2 <a href="https://wg21.link/rand.util.canonical">[rand.util.canonical]</a>.
</p>

<p><i>[2013-04-20, Bristol]</i></p>


<p>
Open an editorial issue on the exception wording ("Throws: What and when"). 
<p/>
Solution: move to tentatively ready. 
</p>

<p><i>[2013-09-29, Chicago]</i></p>


<p>
Apply to Working Paper
</p>


<p id="res-2180"><b>Proposed resolution:</b></p>
<p>This wording is relative to N3376.</p>

<ol>
<li><p>Edit 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> p1 as indicated:</p>

<blockquote>
<p>
<del>-1- No function described in this section 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> throws an exception.</del>
</p>
</blockquote>
</li>

<li><p>Edit 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> around p2 as indicated:</p>

<blockquote><pre>
seed_seq();
</pre><blockquote>
<p>
-2- <i>Effects</i>: Constructs a <code>seed_seq</code> object as if by default-constructing its member <code>v</code>.
<p/>
<ins>-?- <i>Throws</i>: Nothing.</ins>
</p>
</blockquote></blockquote>
</li>

<li><p>Edit 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> around p7 as indicated:</p>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void generate(RandomAccessIterator begin, RandomAccessIterator end);
</pre><blockquote>
<p>
-7- <i>Requires</i>: <code>RandomAccessIterator</code> shall meet the requirements of a mutable random access iterator
(Table 111) type. Moreover, <code>iterator_traits&lt;class RandomAccessIterator&gt;::value_type</code> shall denote
an unsigned integer type capable of accommodating 32-bit quantities.
<p/>
-8- <i>Effects</i>: Does nothing if <code>begin == end</code>. Otherwise, with <code>s = v.size()</code> and 
<code>n = end - begin</code>, fills the supplied range <code>[begin, end)</code> according to the following algorithm [&hellip;]
<p/>
<ins>-?- <i>Throws</i>: What and when <code>RandomAccessIterator</code> operations of <code>begin</code> and <code>end</code> throw.</ins>
</p>
</blockquote></blockquote>
</li>

<li><p>Edit 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> around p9 as indicated:</p>

<blockquote><pre>
size_t size() const;
</pre><blockquote>
<p>
-9- <i>Returns</i>: The number of 32-bit units that would be returned by a call to <code>param()</code>.
<p/>
<ins>-??- <i>Throws</i>: Nothing.</ins>
<p/>
-10- <i>Complexity</i>: constant time.
</p>
</blockquote></blockquote>
</li>

<li><p>Edit 29.5.8.1 <a href="https://wg21.link/rand.util.seedseq">[rand.util.seedseq]</a> around p11 as indicated:</p>

<blockquote><pre>
template&lt;class OutputIterator&gt;
  void param(OutputIterator dest) const;
</pre><blockquote>
<p>
-11- <i>Requires</i>: <code>OutputIterator</code> shall satisfy the requirements of an output iterator (Table 108) type. 
Moreover, the expression <code>*dest = rt</code> shall be valid for a value <code>rt</code> of type <code>result_type</code>.
<p/>
-12- <i>Effects</i>: Copies the sequence of prepared 32-bit units to the given destination, as if by executing the
following statement:
</p>
<blockquote><pre>
copy(v.begin(), v.end(), dest);
</pre></blockquote>
<p>
<ins>-??- <i>Throws</i>: What and when <code>OutputIterator</code> operations of <code>dest</code> throw.</ins>
</p>
</blockquote></blockquote>
</li>

</ol>






</body>
</html>
