Skip to content

Commit 9453067

Browse files
committed
bugfix not compiling when giving string itoa a bool
1 parent 09b89b0 commit 9453067

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

klib/string.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ namespace klib::string::detail {
310310
return 0;
311311
}
312312

313+
// helper to avoid make_unsigned_t<bool> which is ill-formed in C++17 and later
314+
template <typename T>
315+
using safe_unsigned_t = std::make_unsigned_t<std::conditional_t<std::is_same_v<T, bool>, uint8_t, T>>;
316+
313317
template <
314318
base B = _default_base,
315319
typename T = int,
@@ -335,7 +339,7 @@ namespace klib::string::detail {
335339
}
336340
}
337341

338-
while (static_cast<std::make_unsigned_t<T>>(value) > 0) {
342+
while (static_cast<safe_unsigned_t<T>>(value) > 0) {
339343
chars += 1;
340344

341345
if constexpr (B == base::BIN) {
@@ -415,11 +419,16 @@ namespace klib::string::detail {
415419
}
416420
}
417421

418-
template <base B = _default_base, bool BoolAlpha = _default_boolalpha, typename T = int>
422+
template <
423+
base B = _default_base,
424+
bool BoolAlpha = _default_boolalpha,
425+
typename T = int,
426+
typename = std::enable_if_t<std::is_integral_v<T>>
427+
>
419428
constexpr void itoa_impl(T value, char *const str) {
420429
// handle the boolalpha case first
421430
if constexpr (BoolAlpha) {
422-
if (value == 0) {
431+
if (!value) {
423432
strcpy(str, "false");
424433
}
425434
else {
@@ -471,7 +480,7 @@ namespace klib::string::detail {
471480
// loop until we dont have any more characters left
472481
for (uint32_t i = 0; i < count; i++) {
473482
// get the remainder
474-
const T remainder = static_cast<std::make_unsigned_t<T>>(value) % b;
483+
const T remainder = static_cast<safe_unsigned_t<T>>(value) % b;
475484

476485
// add the letter to the string
477486
if (remainder > 9) {

0 commit comments

Comments
 (0)