Skip to content

Commit f2d7d7d

Browse files
committed
🐛 Allow UDL to work with digit separators
Problem: - The `_c` UDL doesn't deal with digit separators. Solution: - Fix it.
1 parent 1d0940c commit f2d7d7d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

include/stdx/udls.hpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,26 @@ namespace stdx {
1111
inline namespace v1 {
1212

1313
namespace detail {
14+
template <char C>
15+
constexpr static bool is_decimal_digit_v = C >= '0' and C <= '9';
16+
template <char C> constexpr static bool is_digit_sep_v = C == '\'';
17+
18+
template <char C, typename Sum> CONSTEVAL auto maybe_add_digit(Sum s) {
19+
if constexpr (is_decimal_digit_v<C>) {
20+
s *= 10;
21+
s += C - '0';
22+
}
23+
return s;
24+
}
25+
1426
template <typename T, char... Chars> CONSTEVAL auto decimal() -> T {
15-
static_assert((... and (Chars >= '0' and Chars <= '9')),
16-
"decimal numbers only are supported");
27+
static_assert(
28+
(... and (is_decimal_digit_v<Chars> or is_digit_sep_v<Chars>)),
29+
"decimal numbers only are supported");
1730
using U = decltype(stdx::to_underlying(std::declval<T>()));
31+
1832
auto x = U{};
19-
((x *= 10, x += Chars - '0'), ...);
33+
((x = maybe_add_digit<Chars>(x)), ...);
2034
return T{x};
2135
}
2236
} // namespace detail

test/udls.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ TEST_CASE("compile-time constant", "[units]") {
5050
std::integral_constant<unsigned int, 0> const>);
5151
static_assert(std::is_same_v<decltype(0_c),
5252
std::integral_constant<std::uint32_t, 0>>);
53+
static_assert(
54+
std::is_same_v<decltype(123'456_c),
55+
std::integral_constant<std::uint32_t, 123'456>>);
5356
}
5457

5558
namespace {

0 commit comments

Comments
 (0)