From e88d6e071df2354f766d8caa76c843855ca834a4 Mon Sep 17 00:00:00 2001 From: Jiaming Yuan Date: Thu, 30 Jun 2022 01:13:22 +0800 Subject: [PATCH] Fix compiler warning in JSON IO. (#8031) Co-authored-by: Hyunsu Cho --- include/xgboost/json_io.h | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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); }