diff --git a/include/xgboost/json_io.h b/include/xgboost/json_io.h index c5eb268be..742231055 100644 --- a/include/xgboost/json_io.h +++ b/include/xgboost/json_io.h @@ -17,6 +17,22 @@ #include namespace xgboost { +namespace detail { +// Whether char is signed is undefined, as a result we might or might not need +// static_cast and std::to_string. +template ::value>* = nullptr> +std::string CharToStr(Char c) { + static_assert(std::is_same::value, ""); + return std::string{c}; +} + +template ::value>* = nullptr> +std::string CharToStr(Char c) { + static_assert(std::is_same::value, ""); + return (c <= static_cast(127) ? std::string{c} : std::to_string(c)); +} +} // namespace detail + /* * \brief A json reader, currently error checking and utf-8 is not fully supported. */ @@ -89,7 +105,7 @@ class JsonReader { } else if (got == 0) { msg += "\\0\""; } else { - msg += (got <= static_cast(127) ? std::string{got} : std::to_string(got)) + " \""; + msg += detail::CharToStr(got) + " \""; } Error(msg); }