File tree Expand file tree Collapse file tree 3 files changed +22
-8
lines changed Expand file tree Collapse file tree 3 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -121,17 +121,20 @@ static_assert(stdx::bit_pack<std::uint32_t>(a[0], a[1]) == 0x1234'5678u);
121
121
=== `smallest_uint`
122
122
123
123
`smallest_uint` is a function template that selects the smallest unsigned
124
- integral type that will fit a compile-time value .
124
+ integral type that will fit a number of bits .
125
125
126
126
[source,cpp]
127
127
----
128
- constexpr auto x = stdx::smallest_uint<42 >(); // std::uint8_t{42 }
129
- constexpr auto y = stdx::smallest_uint<1337 >(); // std::uint16_t{1337 }
128
+ constexpr auto x = stdx::smallest_uint<4 >(); // std::uint8_t{}
129
+ constexpr auto y = stdx::smallest_uint<9 >(); // std::uint16_t{}
130
130
131
131
// smallest_uint_t is the type of a call to smallest_uint
132
- using T = stdx::smallest_uint_t<1337 >; // std::uint16_t
132
+ using T = stdx::smallest_uint_t<9 >; // std::uint16_t
133
133
----
134
134
135
+ NOTE: Giving `smallest_uint_t` any bit size over 64 will still return a
136
+ `std::uint64_t`.
137
+
135
138
=== `to_be`, `from_be`, `to_le`, `from_le`
136
139
137
140
`to_be` and `from_be` are variations on `byteswap` that do the job of
Original file line number Diff line number Diff line change @@ -421,13 +421,13 @@ template <typename T> constexpr auto bit_size() -> std::size_t {
421
421
422
422
template <std::size_t N> CONSTEVAL auto smallest_uint () {
423
423
if constexpr (N <= std::numeric_limits<std::uint8_t >::digits) {
424
- return std::uint8_t {N };
424
+ return std::uint8_t {};
425
425
} else if constexpr (N <= std::numeric_limits<std::uint16_t >::digits) {
426
- return std::uint16_t {N };
426
+ return std::uint16_t {};
427
427
} else if constexpr (N <= std::numeric_limits<std::uint32_t >::digits) {
428
- return std::uint32_t {N };
428
+ return std::uint32_t {};
429
429
} else {
430
- return std::uint64_t {N };
430
+ return std::uint64_t {};
431
431
}
432
432
}
433
433
Original file line number Diff line number Diff line change @@ -421,3 +421,14 @@ TEST_CASE("bit_pack/unpack round trip 64 <-> 32", "[bit]") {
421
421
auto const [a, b] = stdx::bit_unpack<std::uint32_t >(x);
422
422
CHECK (stdx::bit_pack<std::uint64_t >(a, b) == x);
423
423
}
424
+
425
+ TEST_CASE (" smallest_uint" , " [bit]" ) {
426
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <8 >, std::uint8_t >);
427
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <9 >, std::uint16_t >);
428
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <16 >, std::uint16_t >);
429
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <17 >, std::uint32_t >);
430
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <32 >, std::uint32_t >);
431
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <33 >, std::uint64_t >);
432
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <64 >, std::uint64_t >);
433
+ STATIC_REQUIRE (std::is_same_v<stdx::smallest_uint_t <65 >, std::uint64_t >);
434
+ }
You can’t perform that action at this time.
0 commit comments