Commit f11d889 1 parent 1d6b68d commit f11d889 Copy full SHA for f11d889
File tree 2 files changed +25
-2
lines changed
2 files changed +25
-2
lines changed Original file line number Diff line number Diff line change @@ -1003,8 +1003,9 @@ struct uint
1003
1003
1004
1004
constexpr explicit operator bool () const noexcept { return *this != uint {}; }
1005
1005
1006
+ // / Explicit converting operator to smaller uint types.
1006
1007
template <unsigned M>
1007
- explicit operator uint <M>() const noexcept
1008
+ constexpr explicit operator uint <M>() const noexcept
1008
1009
requires (M < N)
1009
1010
{
1010
1011
uint <M> r;
@@ -1015,7 +1016,7 @@ struct uint
1015
1016
1016
1017
// / Explicit converting operator for all builtin integral types.
1017
1018
template <typename Int>
1018
- explicit operator Int () const noexcept
1019
+ constexpr explicit operator Int () const noexcept
1019
1020
requires(std::is_integral_v<Int>)
1020
1021
{
1021
1022
static_assert (sizeof (Int) <= sizeof (uint64_t ));
Original file line number Diff line number Diff line change @@ -292,8 +292,30 @@ TYPED_TEST(uint_api, bitwise_op_assignment)
292
292
EXPECT_EQ (x >>= x, 0 );
293
293
}
294
294
295
+ TYPED_TEST (uint_api, explicit_conversion_to_smaller_uint)
296
+ {
297
+ if constexpr (TypeParam::num_bits > 128 )
298
+ {
299
+ using SmallerType = intx::uint <TypeParam::num_bits - 64 >;
300
+ static_assert (static_cast <SmallerType>(TypeParam{1 }) == 1 );
301
+
302
+ TypeParam x;
303
+ for (uint64_t i = 0 ; i < TypeParam::num_words; ++i)
304
+ x[i] = i + 1 ;
305
+
306
+ const auto smaller = static_cast <SmallerType>(x);
307
+ for (uint64_t i = 0 ; i < SmallerType::num_words; ++i)
308
+ {
309
+ EXPECT_EQ (smaller[i], i + 1 );
310
+ }
311
+ }
312
+ }
313
+
295
314
TYPED_TEST (uint_api, explicit_conversion_to_integral_type)
296
315
{
316
+ static_assert (
317
+ static_cast <uint32_t >(TypeParam{0x0102030405060708 , 0xffffffffffffffff }) == 0x05060708 );
318
+
297
319
TypeParam x;
298
320
x[0 ] = 3 ;
299
321
x[1 ] = 0xffffffffffffffff ;
You can’t perform that action at this time.
0 commit comments