<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2326: uniform_int_distribution&lt;unsigned char&gt; should be permitted</title>
<meta property="og:title" content="Issue 2326: uniform_int_distribution&lt;unsigned char&gt; should be permitted">
<meta property="og:description" content="C++ library issue. Status: NAD">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2326.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#NAD">NAD</a> status.</em></p>
<h3 id="2326"><a href="lwg-closed.html#2326">2326</a>. <code>uniform_int_distribution&lt;unsigned char&gt;</code> should be permitted</h3>
<p><b>Section:</b> 29.5.3.1 <a href="https://wg21.link/rand.req.genl">[rand.req.genl]</a> <b>Status:</b> <a href="lwg-active.html#NAD">NAD</a>
 <b>Submitter:</b> Stephan T. Lavavej <b>Opened:</b> 2013-09-21 <b>Last modified:</b> 2017-02-02</p>
<p><b>Priority: </b>2
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.req.genl">issues</a> in [rand.req.genl].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#NAD">NAD</a> status.</p>
<p><b>Discussion:</b></p>
<p>
29.5.3.1 <a href="https://wg21.link/rand.req.genl">[rand.req.genl]</a>/1 says: "Throughout this subclause 26.5, the effect of instantiating a template [...] that has a 
template type parameter named <code>IntType</code> is undefined unless the corresponding template argument is <i>cv</i>-unqualified 
and is one of <code>short</code>, <code>int</code>, <code>long</code>, <code>long long</code>, <code>unsigned short</code>, <code>unsigned int</code>, 
<code>unsigned long</code>, or <code>unsigned long long</code>."  29.5.9.2.1 <a href="https://wg21.link/rand.dist.uni.int">[rand.dist.uni.int]</a> specifies 
<code>template&lt;class IntType = int> class uniform_int_distribution</code>, so this forbids 
<code>uniform_int_distribution&lt;char/signed char/unsigned char&gt;</code>.
<p/>
I am not aware of anything in <code>&lt;random&gt;</code> that works with 16-bit integers but fails with 8-bit integers, so I suspect 
that <code>IntType</code> and <code>UIntType</code> could simply be extended to permit the <code>char</code> family. Alternatively, this 
change could be limited to <code>uniform_int_distribution</code> alone, where it is definitely safe. A <code>&lt;random&gt;</code> expert 
should decide which change is best.
</p>

<p><i>[2015-04-04 Geoffrey provides wording]</i></p>

<p>
 I think it's time to call the question; it's just silly that we have a random number library with no natural way to generate 
 random bytes. However, I don't think it's sufficient to fix only <code>uniform_int_distribution</code>, or even all of <code>IntType</code>. 
 At a bare minimum we need to also fix <code>independent_bits_engine</code> (arguably the cleanest way of generating a random byte) 
 and that's specified in terms of <code>UIntType</code>.
 <p/>
 The wording provided below is equivalent to adding <code>unsigned char</code> to item "f" and adding <code>signed char</code> and 
 <code>unsigned char</code> to item "e". That means it still excludes <code>char</code>, but I'm OK with that. If you want to generate 
 a 1-byte number, you should probably pick a signedess, and if you want to generate a raw byte, the "true" raw byte type is 
 <code>unsigned char</code>. This also excludes extended integral types and wide char types, which seem like nice-to-haves at best. 
 I have no objection to supporting any of those types; I just picked this to simplify the wording and hopefully maximize consensus. 
 Note that if we want to broaden <code>IntType</code> to permit any integral type, we'll need to decide if we want to exclude <code>bool</code>.
<p/>
For reference, <code>IntType</code> is used as a parameter of the following templates:
</p>
<blockquote>
<pre>
uniform_int_distribution
binomial_distribution
geometric_distribution
negative_binomial_distribution
poisson_distribution
discrete_distribution
</pre>
</blockquote>
<p>
and <code>UIntType</code> is used as a parameter of the following templates:
</p>
<blockquote>
<pre>
linear_congruential_engine
mersenne_twister_engine
subtract_with_carry_engine
independent_bits_engine
</pre>
</blockquote>

<p><i>[2015-9-11, Telecon]</i></p>

<p>Walter feels very strongly that this is not a defect, but a feature request.</p>
<p>In a previous telecon, Aaron offered to write a paper proposing this.</p>
<p>Jonathan offered to help.</p>

<p>Closing as NAD</p>


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

<ol>
<li><p>Change in 29.5.3.1 <a href="https://wg21.link/rand.req.genl">[rand.req.genl]</a> p1 as indicated:</p>

<p>
-1- Throughout this subclause 26.5, the effect of instantiating a template:
</p>
<ol style="list-style-type:lower-alpha;" start="4">
<li><p>[&hellip;]</p></li>
<li><p>that has a template type parameter named <code>IntType</code> is undefined unless the corresponding template
argument is cv-unqualified and is <ins>a standard integer type (6.9.2 <a href="https://wg21.link/basic.fundamental">[basic.fundamental]</a>)</ins><del>one of 
<code>short</code>, <code>int</code>, <code>long</code>, <code>long long</code>, <code>unsigned short</code>, <code>unsigned int</code>, 
<code>unsigned long</code>, or <code>unsigned long long</code></del>.</p></li>
<li><p>that has a template type parameter named <code>UIntType</code> is undefined unless the corresponding template
argument is cv-unqualified and is <ins>a standard unsigned integer type (6.9.2 <a href="https://wg21.link/basic.fundamental">[basic.fundamental]</a>)</ins><del>one 
of <code>unsigned short</code>, <code>unsigned int</code>, <code>unsigned long</code>, or <code>unsigned long long</code></del>.</p></li>
</ol>
</li>
</ol>





</body>
</html>
