<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 654: Missing IO roundtrip for random number engines</title>
<meta property="og:title" content="Issue 654: Missing IO roundtrip for random number engines">
<meta property="og:description" content="C++ library issue. Status: CD1">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue654.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#CD1">CD1</a> status.</em></p>
<h3 id="654"><a href="lwg-defects.html#654">654</a>. Missing IO roundtrip for random number engines</h3>
<p><b>Section:</b> 29.5.3.4 <a href="https://wg21.link/rand.req.eng">[rand.req.eng]</a> <b>Status:</b> <a href="lwg-active.html#CD1">CD1</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2007-03-08 <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.req.eng">issues</a> in [rand.req.eng].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#CD1">CD1</a> status.</p>
<p><b>Discussion:</b></p>
<p>
Table 98 and para 5 in 29.5.3.4 <a href="https://wg21.link/rand.req.eng">[rand.req.eng]</a> specify
the IO insertion and extraction semantic of random
number engines. It can be shown, v.i., that the specification
of the extractor cannot guarantee to fulfill the requirement
from para 5:
</p>

<blockquote><p>
If a textual representation written via os &lt;&lt; x was
subsequently read via is &gt;&gt; v, then x == v provided that
there have been no intervening invocations of x or of v.
</p></blockquote>

<p>
The problem is, that the extraction process described in
table 98 misses to specify that it will initially set the
if.fmtflags to ios_base::dec, see table 104:
</p>

<blockquote><p>
dec: converts integer input or generates integer output
in decimal base
</p></blockquote>

<p>
Proof: The following small program demonstrates the violation
of requirements (exception safety not fulfilled):
</p>

<blockquote><pre>
#include &lt;cassert&gt;
#include &lt;ostream&gt;
#include &lt;iostream&gt;
#include &lt;iomanip&gt;
#include &lt;sstream&gt;

class RanNumEngine {
  int state;
public:
  RanNumEngine() : state(42) {}

  bool operator==(RanNumEngine other) const {
      return state == other.state;
  }

  template &lt;typename Ch, typename Tr&gt;
  friend std::basic_ostream&lt;Ch, Tr&gt;&amp; operator&lt;&lt;(std::basic_ostream&lt;Ch, Tr&gt;&amp; os, RanNumEngine engine) {
    Ch old = os.fill(os.widen(' ')); // Sets space character
    std::ios_base::fmtflags f = os.flags();
    os &lt;&lt; std::dec &lt;&lt; std::left &lt;&lt; engine.state; // Adds ios_base::dec|ios_base::left
    os.fill(old); // Undo
    os.flags(f);
    return os;
  }

  template &lt;typename Ch, typename Tr&gt;
  friend std::basic_istream&lt;Ch, Tr&gt;&amp; operator&gt;&gt;(std::basic_istream&lt;Ch, Tr&gt;&amp; is, RanNumEngine&amp; engine) {
       // Uncomment only for the fix.

    //std::ios_base::fmtflags f = is.flags();
    //is &gt;&gt; std::dec;
    is &gt;&gt; engine.state;
    //is.flags(f);
    return is;
  }
};

int main() {
    std::stringstream s;
    s &lt;&lt; std::setfill('#'); // No problem
        s &lt;&lt; std::oct; // Yikes!
        // Here starts para 5 requirements:
    RanNumEngine x;
    s &lt;&lt; x;
    RanNumEngine v;
    s &gt;&gt; v;
    assert(x == v); // Fails: 42 == 34
}
</pre></blockquote>

<p>
A second, minor issue seems to be, that the insertion
description from table 98 unnecessarily requires the
addition of ios_base::fixed (which only influences floating-point
numbers). Its not entirely clear to me whether the proposed
standard does require that the state of random number engines
is stored in integral types or not, but I have the impression
that this is the indent, see e.g. p. 3
</p>

<blockquote><p>
The specification of each random number engine defines the
size of its state in multiples of the size of its result_type.
</p></blockquote>

<p>
If other types than integrals are supported, then I wonder why
no requirements are specified for the precision of the stream.
</p>

<p>
See <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2391.pdf">N2391</a> and
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2423.pdf">N2423</a>
for some further discussion.
</p>


<p id="res-654"><b>Proposed resolution:</b></p>
<p>
Adopt the proposed resolution in
<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2423.pdf">N2423</a>.
</p>


<p><i>[
Kona (2007): The LWG adopted the proposed resolution of N2423 for this issue.
The LWG voted to accelerate this issue to Ready status to be voted into the WP at Kona.
]</i></p>





</body>
</html>
