File tree Expand file tree Collapse file tree 2 files changed +20
-3
lines changed
Expand file tree Collapse file tree 2 files changed +20
-3
lines changed Original file line number Diff line number Diff line change @@ -11,12 +11,26 @@ namespace stdx {
1111inline namespace v1 {
1212
1313namespace 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+
1426template <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
Original file line number Diff line number Diff 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
5558namespace {
You can’t perform that action at this time.
0 commit comments