This page is a snapshot from the LWG issues list, see the Library Active Issues List for more information and the meaning of WP status.

3809. Is std::subtract_with_carry_engine<uint16_t> supposed to work?

Section: 29.5.4.4 [rand.eng.sub] Status: WP Submitter: Jonathan Wakely Opened: 2022-11-02 Last modified: 2023-11-22

Priority: 3

View all other issues in [rand.eng.sub].

View all issues with WP status.

Discussion:

The standard requires subtract_with_carry_engine<T> to use:

linear_congruential_engine<T, 40014u, 0u, 2147483563u>

where each of those values is converted to T.

This appears to mean subtract_with_carry_engine cannot be used with uint16_t, because 2147483563u cannot be converted to uint16_t without narrowing.

What is the intention here? Should it be ill-formed? Should the seed engine be linear_congruential_engine<uint_least32_t, …> instead? The values from the linear_congruential_engine are used modulo 2^32 so getting 64-bit values from it is pointless, and getting 16-bit values from it doesn't compile.

[Kona 2022-11-12; Set priority to 3]

[Kona 2022-11-25; Jonathan provides wording]

[2023-05; reflector poll]

Status to Tentatively Ready after six votes in favour.

[2023-11-11 Approved at November 2023 meeting in Kona. Status changed: Voting → WP.]

Proposed resolution:

This wording is relative to N4917.

  1. Modify the class synopsis in 29.5.4.4 [rand.eng.sub] as indicated:

    namespace std {
      template<class UIntType, size_t w, size_t s, size_t r>
      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 result_typeuint_least32_t default_seed = 19780503u;
        // constructors and seeding functions
        subtract_with_carry_engine() : subtract_with_carry_engine(default_seed0u) {}
        explicit subtract_with_carry_engine(result_type value);
        template<class Sseq> explicit subtract_with_carry_engine(Sseq& q);
        void seed(result_type value = default_seed0u);
        template<class Sseq> void seed(Sseq& q);
    
  2. Modify 29.5.4.4 [rand.eng.sub] p7 as indicated:

    explicit subtract_with_carry_engine(result_type value);

    -7- Effects: Sets the values of X r , , X 1 , in that order, as specified below. If X 1 is then 0 , sets c to 1 ; otherwise sets c to 0 .

         To set the values X k , first construct e, a linear_congruential_engine object, as if by the following definition:

    linear_congruential_engine<result_typeuint_least32_t,
                               40014u,0u,2147483563u> e(value == 0u ? default_seed : value);
    

         Then, to set each X k , obtain new values z 0 , , z n 1 from n = w 32 successive invocations of e. Set X k to ( j = 0 n 1 z j 2 32 j ) mod m .