Skip to content

Commit 4b22dd6

Browse files
<random>: Improvements around static constexpr member variables (#3006)
1 parent 9475fa6 commit 4b22dd6

File tree

1 file changed

+16
-36
lines changed

1 file changed

+16
-36
lines changed

stl/inc/random

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -790,12 +790,6 @@ protected:
790790
typename _Swc_Traits::_Cy_t _Carry;
791791
};
792792

793-
template <class _Ty, size_t _Sx, size_t _Rx, class _Swc_Traits>
794-
const size_t _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::short_lag;
795-
796-
template <class _Ty, size_t _Sx, size_t _Rx, class _Swc_Traits>
797-
const size_t _Swc_base<_Ty, _Sx, _Rx, _Swc_Traits>::long_lag;
798-
799793
template <class _Ty, _Ty _Mx, size_t _Nw>
800794
struct _Swc_traits { // traits for subtract_with_carry generator
801795
using _Cy_t = int;
@@ -1005,16 +999,28 @@ public:
1005999
};
10061000

10071001
#if _HAS_TR1_NAMESPACE
1002+
_CONSTEVAL double _Cx_exp2(const int _Exp) noexcept {
1003+
double _Ret = 1.0;
1004+
for (int _Count = _Exp; _Count > 0; --_Count) {
1005+
_Ret *= 2.0;
1006+
}
1007+
for (int _Count = _Exp; _Count < 0; ++_Count) {
1008+
_Ret *= 0.5;
1009+
}
1010+
1011+
return _Ret;
1012+
}
1013+
10081014
template <class _Ty, size_t _Wx, size_t _Rx>
10091015
struct _Swc_01_traits { // traits for subtract_with_carry_01 generator
10101016
using _Cy_t = _Ty;
10111017
using _UCy_t = _Ty;
10121018
using _Mod_t = _Ty;
10131019
using _Seed_t = unsigned int;
10141020

1015-
static const _Cy_t _Cy;
1016-
static const _Mod_t _Mod;
1017-
static const _Ty _Max;
1021+
static constexpr _Cy_t _Cy = static_cast<_Cy_t>(_Cx_exp2(static_cast<int>(-static_cast<ptrdiff_t>(_Wx))));
1022+
static constexpr _Mod_t _Mod = 1;
1023+
static constexpr _Ty _Max = 1;
10181024
static constexpr int _Nwords = (_Wx + 31) / 32;
10191025

10201026
template <class _Gen>
@@ -1058,24 +1064,10 @@ struct _Swc_01_traits { // traits for subtract_with_carry_01 generator
10581064
}
10591065

10601066
private:
1061-
static const _Ty _Scale1;
1067+
static constexpr _Ty _Scale1 = static_cast<_Ty>(_Cx_exp2(_Wx % 32));
10621068
static constexpr unsigned long _Mask = ~((~0UL) << (_Wx % 32));
10631069
};
10641070

1065-
template <class _Ty, size_t _Wx, size_t _Rx>
1066-
const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t
1067-
_Swc_01_traits<_Ty, _Wx, _Rx>::_Cy = static_cast<typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Cy_t>(
1068-
_CSTD ldexp(1.0, static_cast<int>(-static_cast<ptrdiff_t>(_Wx))));
1069-
1070-
template <class _Ty, size_t _Wx, size_t _Rx>
1071-
const typename _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod_t _Swc_01_traits<_Ty, _Wx, _Rx>::_Mod = 1;
1072-
1073-
template <class _Ty, size_t _Wx, size_t _Rx>
1074-
const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Max = 1;
1075-
1076-
template <class _Ty, size_t _Wx, size_t _Rx>
1077-
const _Ty _Swc_01_traits<_Ty, _Wx, _Rx>::_Scale1 = static_cast<_Ty>(_CSTD ldexp(1.0, _Wx % 32));
1078-
10791071
template <class _Ty, size_t _Wx, size_t _Sx, size_t _Rx>
10801072
class _DEPRECATE_TR1_NAMESPACE subtract_with_carry_01
10811073
: public _Swc_base<_Ty, _Sx, _Rx, _Swc_01_traits<_Ty, _Wx, _Rx>> { // subtract_with_carry_01 generator
@@ -1091,12 +1083,6 @@ public:
10911083
template <class _Gen, _Enable_if_seed_seq_t<_Gen, subtract_with_carry_01> = 0>
10921084
subtract_with_carry_01(_Gen& _Gx) : _Mybase(_Gx) {}
10931085
};
1094-
1095-
_STL_DISABLE_DEPRECATED_WARNING
1096-
template <class _Ty, size_t _Wx, size_t _Sx, size_t _Rx>
1097-
const size_t subtract_with_carry_01<_Ty, _Wx, _Sx, _Rx>::word_size;
1098-
_STL_RESTORE_DEPRECATED_WARNING
1099-
11001086
#endif // _HAS_TR1_NAMESPACE
11011087

11021088
template <class _Ty, int _Wx, int _Nx, int _Mx, int _Rx, _Ty _Px, int _Ux, int _Sx, _Ty _Bx, int _Tx, _Ty _Cx, int _Lx>
@@ -1443,12 +1429,6 @@ private:
14431429
int _Nx;
14441430
};
14451431

1446-
template <class _Engine, int _Px, int _Rx>
1447-
const int discard_block<_Engine, _Px, _Rx>::block_size;
1448-
1449-
template <class _Engine, int _Px, int _Rx>
1450-
const int discard_block<_Engine, _Px, _Rx>::used_block;
1451-
14521432
template <class _Engine, size_t _Px, size_t _Rx>
14531433
class discard_block_engine : public discard_block<_Engine, _Px, _Rx> { // discard_block_engine compound engine
14541434
public:

0 commit comments

Comments
 (0)