I came across this post: Convert a number to a string literal with constexpr
And the answer is quite interesting:
namespace detail
{
template<unsigned... digits>
struct to_chars { static const char value[]; };
template<unsigned... digits>
const char to_chars<digits...>::value[] = {('0' + digits)..., 0};
template<unsigned rem, unsigned... digits>
struct explode : explode<rem / 10, rem % 10, digits...> {};
template<unsigned... digits>
struct explode<0, digits...> : to_chars<digits...> {};
}
template<unsigned num>
struct num_to_string : detail::explode<num> {};
My questions are:
-
"struct explode : explode" declares explode inherits from explode; how about "struct explode<0, digits...> : to_chars"?
-
What's the function for the '0' as the first template parameter?
Thanks!
Aucun commentaire:
Enregistrer un commentaire