<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3809: Is std::subtract_with_carry_engine&lt;uint16_t&gt; supposed to work?</title>
<meta property="og:title" content="Issue 3809: Is std::subtract_with_carry_engine&lt;uint16_t&gt; supposed to work?">
<meta property="og:description" content="C++ library issue. Status: WP">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3809.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#WP">WP</a> status.</em></p>
<h3 id="3809"><a href="lwg-defects.html#3809">3809</a>. Is <code>std::subtract_with_carry_engine&lt;uint16_t&gt;</code> supposed to work?</h3>
<p><b>Section:</b> 29.5.4.4 <a href="https://wg21.link/rand.eng.sub">[rand.eng.sub]</a> <b>Status:</b> <a href="lwg-active.html#WP">WP</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2022-11-02 <b>Last modified:</b> 2025-09-08</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#rand.eng.sub">issues</a> in [rand.eng.sub].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#WP">WP</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The standard requires <code>subtract_with_carry_engine&lt;T&gt;</code> to use:
</p>
<blockquote><pre>
linear_congruential_engine&lt;T, 40014u, 0u, 2147483563u&gt;
</pre></blockquote>
<p>
where each of those values is converted to <code>T</code>.
<p/>
This appears to mean <code>subtract_with_carry_engine</code> cannot be used with <code>uint16_t</code>, because 
<code>2147483563u</code> cannot be converted to <code>uint16_t</code> without narrowing.
<p/>
What is the intention here? Should it be ill-formed? Should the seed engine be 
<code>linear_congruential_engine&lt;uint_least32_t, &hellip;&gt;</code> instead? The values from the 
<code>linear_congruential_engine</code> are used modulo 2^32 so getting 64-bit values from it is pointless, 
and getting 16-bit values from it doesn't compile.
</p>

<p><i>[Kona 2022-11-12; Set priority to 3]</i></p>


<p><i>[Kona 2022-11-25; Jonathan provides wording]</i></p>


<p><i>[2023-05; reflector poll]</i></p>

<p>Status to Tentatively Ready after six votes in favour.</p>

<p><i>[2023-11-11 Approved at November 2023 meeting in Kona. Status changed: Voting &rarr; WP.]</i></p>


<p><i>[This resolution had to be fixed by <a href="lwg-defects.html#4014" title="LWG 3809 changes behavior of some existing std::subtract_with_carry_engine code (Status: WP)">4014</a><sup><a href="https://cplusplus.github.io/LWG/issue4014" title="Latest snapshot">(i)</a></sup>]</i></p>




<p id="res-3809"><b>Proposed resolution:</b></p>

<p>This wording is relative to <a href="https://wg21.link/N4917" title=" Working Draft, Standard for Programming Language C++">N4917</a>.</p>

<ol>
<li>
<p>Modify the class synopsis in 29.5.4.4 <a href="https://wg21.link/rand.eng.sub">[rand.eng.sub]</a> as indicated:</p>
<blockquote><pre>
namespace std {
  template&lt;class UIntType, size_t w, size_t s, size_t r&gt;
  class subtract_with_carry_engine {
  public:
    // types
    using result_type = UIntType;
    // engine characteristics
    static constexpr size_t word_size = w;
    static constexpr size_t short_lag = s;
    static constexpr size_t long_lag = r;
    static constexpr result_type min() { return 0; }
    static constexpr result_type max() { return m − 1; }
    static constexpr <del>result_type</del><ins>uint_least32_t</ins> default_seed = 19780503u;
    // constructors and seeding functions
    subtract_with_carry_engine() : subtract_with_carry_engine(<del>default_seed</del><ins>0u</ins>) {}
    explicit subtract_with_carry_engine(result_type value);
    template&lt;class Sseq&gt; explicit subtract_with_carry_engine(Sseq&amp; q);
    void seed(result_type value = <del>default_seed</del><ins>0u</ins>);
    template&lt;class Sseq&gt; void seed(Sseq&amp; q);
</pre></blockquote>
</li>
<li>
<p>Modify 29.5.4.4 <a href="https://wg21.link/rand.eng.sub">[rand.eng.sub]</a> p7 as indicated:</p>
<blockquote>
<pre>explicit subtract_with_carry_engine(result_type value);</pre>
<p>-7- <i>Effects</i>:
Sets the values of
<math>
  <mrow>
    <msub> <mi>X</mi> <mrow> <mo>−</mo> <mi>r</mi> </mrow> </msub>
    <mo separator="true">,</mo>
    <mo>&hellip;</mo>
    <mo separator="true">,</mo>
    <msub> <mi>X</mi> <mrow> <mo>−</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>,
in that order, as specified below. If
<math>
  <mrow>
    <msub> <mi>X</mi> <mrow> <mo>−</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>
is then <math> <mn>0</mn> </math>,
sets <math> <mi>c</mi> </math> to <math> <mn>1</mn> </math>;
otherwise sets <math> <mi>c</mi> </math> to <math> <mn>0</mn> </math>.
</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;
To set the values
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>,
first construct <code>e</code>, a <code>linear_congruential_engine</code> object,
as if by the following definition:
</p>
<blockquote><pre>
linear_congruential_engine&lt;<del>result_type</del><ins>uint_least32_t</ins>,
                           40014u,0u,2147483563u&gt; e(value == 0u ? default_seed : value);
</pre></blockquote>
<p>&nbsp;&nbsp;&nbsp;&nbsp;
Then, to set each
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>,
obtain new values
<math>
  <mrow>
    <msub> <mi>z</mi> <mn>0</mn> </msub>
    <mo separator="true">,</mo>
    <mo>&hellip;</mo>
    <mo separator="true">,</mo>
    <msub> <mi>z</mi> <mrow> <mi>n</mi> <mo>−</mo> <mn>1</mn> </mrow> </msub>
  </mrow>
</math>
from
<math>
  <mrow>
    <mi>n</mi> <mo>=</mo>
    <mo form="prefix" stretchy="false">&#x2308;</mo>
    <mi>w</mi> <mo lspace="0em" rspace="0em">⁄</mo> <mn>32</mn>
    <mo form="postfix" stretchy="false">&#x2309;</mo>
  </mrow>
</math>
successive invocations of <code>e</code>.
Set
<math> <mrow> <msub> <mi>X</mi> <mi>k</mi> </msub> </mrow> </math>
to
<math>
  <mrow>
    <mrow>
      <mo fence="true" form="prefix">(</mo>
      <msubsup>
        <mo movablelimits="false">&sum;</mo>
        <mrow> <mi>j</mi> <mo>=</mo> <mn>0</mn> </mrow>
        <mrow> <mi>n</mi> <mo>−</mo> <mn>1</mn> </mrow>
      </msubsup>
      <msub> <mi>z</mi> <mi>j</mi> </msub>
      <mo>&#x2219;</mo>
      <msup> <mn>2</mn> <mrow> <mn>32</mn> <mi>j</mi> </mrow> </msup>
      <mo fence="true" form="postfix">)</mo>
    </mrow>
    <mo lspace="0.2222em" rspace="0.2222em">mod</mo>
    <mi>m</mi>
  </mrow>
</math>.
</p>
</blockquote>
</li>
</ol>





</body>
</html>
