This implements the proposed resolution of LWG 3809, so that
std::subtract_with_carry_engine can be used with a 16-bit result_type.
Currently this produces a narrowing error when instantiating the
std::linear_congruential_engine to create the initial state. It also
truncates the default_seed constant when passing it as a result_type
argument.
Change the type of the constant to uint_least32_t and pass 0u when the
default_seed should be used.
libstdc++-v3/ChangeLog:
PR libstdc++/107466
* include/bits/random.h (subtract_with_carry_engine): Use 32-bit
type for default seed. Use 0u as default argument for
subtract_with_carry_engine(result_type) constructor and
seed(result_type) member function.
* include/bits/random.tcc (subtract_with_carry_engine): Use
32-bit type for default seed and engine used for initial state.
* testsuite/26_numerics/random/subtract_with_carry_engine/cons/lwg3809.cc:
New test.
(cherry picked from commit
a64775a0edd46980036b757041f0c065ed9f8d22)
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 default_seed = 19780503u;
+ static constexpr uint_least32_t default_seed = 19780503u;
- subtract_with_carry_engine() : subtract_with_carry_engine(default_seed)
+ subtract_with_carry_engine() : subtract_with_carry_engine(0u)
{ }
/**
* set carry to 1, otherwise sets carry to 0.
*/
void
- seed(result_type __sd = default_seed);
+ seed(result_type __sd = 0u);
/**
* @brief Seeds the initial state @f$x_0@f$ of the
subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
template<typename _UIntType, size_t __w, size_t __s, size_t __r>
- constexpr _UIntType
+ constexpr uint_least32_t
subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
#endif
subtract_with_carry_engine<_UIntType, __w, __s, __r>::
seed(result_type __value)
{
- std::linear_congruential_engine<result_type, 40014u, 0u, 2147483563u>
+ std::linear_congruential_engine<uint_least32_t, 40014u, 0u, 2147483563u>
__lcg(__value == 0u ? default_seed : __value);
const size_t __n = (__w + 31) / 32;
--- /dev/null
+// { dg-do run { target c++11 } }
+#include <random>
+#include <testsuite_hooks.h>
+
+// LWG 3809. Is std::subtract_with_carry_engine<uint16_t> supposed to work?
+// PRĀ 107466 - invalid -Wnarrowing error with std::subtract_with_carry_engine
+
+int main()
+{
+ // It should be possible to construct this engine with a 16-bit result_type:
+ std::subtract_with_carry_engine<uint16_t, 12, 5, 12> s16;
+ std::subtract_with_carry_engine<uint32_t, 12, 5, 12> s32;
+ // It should have been seeded with the same sequence as the 32-bit version
+ // and produce random numbers in the same range, [0, 1<<12).
+ for (int i = 0; i < 10; ++i)
+ VERIFY( s16() == s32() );
+ // The default seed should be usable without truncation to uint16_t:
+ s16.seed();
+ s32.seed();
+ for (int i = 0; i < 10; ++i)
+ VERIFY( s16() == s32() );
+ s16.seed(101);
+ s32.seed(101);
+ for (int i = 0; i < 10; ++i)
+ VERIFY( s16() == s32() );
+}