Skip to content

Commit f11d889

Browse files
committed
Make remaining simple functions constexpr
1 parent 1d6b68d commit f11d889

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

include/intx/intx.hpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -1003,8 +1003,9 @@ struct uint
10031003

10041004
constexpr explicit operator bool() const noexcept { return *this != uint{}; }
10051005

1006+
/// Explicit converting operator to smaller uint types.
10061007
template <unsigned M>
1007-
explicit operator uint<M>() const noexcept
1008+
constexpr explicit operator uint<M>() const noexcept
10081009
requires(M < N)
10091010
{
10101011
uint<M> r;
@@ -1015,7 +1016,7 @@ struct uint
10151016

10161017
/// Explicit converting operator for all builtin integral types.
10171018
template <typename Int>
1018-
explicit operator Int() const noexcept
1019+
constexpr explicit operator Int() const noexcept
10191020
requires(std::is_integral_v<Int>)
10201021
{
10211022
static_assert(sizeof(Int) <= sizeof(uint64_t));

test/unittests/test_intx_api.cpp

+22
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,30 @@ TYPED_TEST(uint_api, bitwise_op_assignment)
292292
EXPECT_EQ(x >>= x, 0);
293293
}
294294

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+
295314
TYPED_TEST(uint_api, explicit_conversion_to_integral_type)
296315
{
316+
static_assert(
317+
static_cast<uint32_t>(TypeParam{0x0102030405060708, 0xffffffffffffffff}) == 0x05060708);
318+
297319
TypeParam x;
298320
x[0] = 3;
299321
x[1] = 0xffffffffffffffff;

0 commit comments

Comments
 (0)