Skip to content

Commit 278719f

Browse files
authored
Merge pull request #237 from elbeno/fix-smallest-uint-docs
🐛 Fix the docs for `smallest_uint`
2 parents 464a6b4 + b8b20bc commit 278719f

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

docs/bit.adoc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,20 @@ static_assert(stdx::bit_pack<std::uint32_t>(a[0], a[1]) == 0x1234'5678u);
121121
=== `smallest_uint`
122122

123123
`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.
125125

126126
[source,cpp]
127127
----
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{}
130130
131131
// 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
133133
----
134134

135+
NOTE: Giving `smallest_uint_t` any bit size over 64 will still return a
136+
`std::uint64_t`.
137+
135138
=== `to_be`, `from_be`, `to_le`, `from_le`
136139

137140
`to_be` and `from_be` are variations on `byteswap` that do the job of

include/stdx/bit.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,13 @@ template <typename T> constexpr auto bit_size() -> std::size_t {
421421

422422
template <std::size_t N> CONSTEVAL auto smallest_uint() {
423423
if constexpr (N <= std::numeric_limits<std::uint8_t>::digits) {
424-
return std::uint8_t{N};
424+
return std::uint8_t{};
425425
} else if constexpr (N <= std::numeric_limits<std::uint16_t>::digits) {
426-
return std::uint16_t{N};
426+
return std::uint16_t{};
427427
} else if constexpr (N <= std::numeric_limits<std::uint32_t>::digits) {
428-
return std::uint32_t{N};
428+
return std::uint32_t{};
429429
} else {
430-
return std::uint64_t{N};
430+
return std::uint64_t{};
431431
}
432432
}
433433

test/bit.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,14 @@ TEST_CASE("bit_pack/unpack round trip 64 <-> 32", "[bit]") {
421421
auto const [a, b] = stdx::bit_unpack<std::uint32_t>(x);
422422
CHECK(stdx::bit_pack<std::uint64_t>(a, b) == x);
423423
}
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+
}

0 commit comments

Comments
 (0)