Date: Fri, 15 Mar 2024 22:54:52 +0000
Hello all,
Currently std::to_string cannot be overloaded for custom user-defined types. While you could follow the convention of having a to_string free function in the enclosing namespace of a particular type and then rely on ADL to resolve the function, it obviously would not work with fundamental types unless you using-ed namespace std or std::to_string.
I would prefer being able to std::to_string to stringily any type if possible. I believe that it should be based on std::format so that the author of a typer can simply specialise std::formatter<T> to get both std::to_string and std::format/print working.
std::string to_string(std::formattable<char> auto&& value) { return std::format("{}", value); }
Granted, specialising std::formatter<T> is a bit more verbose than simply having a to_string free or member function. Perhaps you could allow formatting, with no format-arguments, a type that just has a to_string free function.
template <typename T>
concept has_free_to_string = requires(T const& t)
{
{ to_string(t) };
};
template <has_free_to_string T>
struct std::formatter<T>
{
auto constexpr parse(std::format_parse_context& context) { return context.begin(); }
auto format(T const& str, std::format_context& context) const
{
return std::format_to(context.out(), "{}", to_string(str));
}
};
The same thing shall apply for wide strings.
Note: I do not have the time to participate in standardisation in order to open formal proposals, but I wish someöne else does based on my ideas.
Regards,
Jaiganésh Kumaran.
Currently std::to_string cannot be overloaded for custom user-defined types. While you could follow the convention of having a to_string free function in the enclosing namespace of a particular type and then rely on ADL to resolve the function, it obviously would not work with fundamental types unless you using-ed namespace std or std::to_string.
I would prefer being able to std::to_string to stringily any type if possible. I believe that it should be based on std::format so that the author of a typer can simply specialise std::formatter<T> to get both std::to_string and std::format/print working.
std::string to_string(std::formattable<char> auto&& value) { return std::format("{}", value); }
Granted, specialising std::formatter<T> is a bit more verbose than simply having a to_string free or member function. Perhaps you could allow formatting, with no format-arguments, a type that just has a to_string free function.
template <typename T>
concept has_free_to_string = requires(T const& t)
{
{ to_string(t) };
};
template <has_free_to_string T>
struct std::formatter<T>
{
auto constexpr parse(std::format_parse_context& context) { return context.begin(); }
auto format(T const& str, std::format_context& context) const
{
return std::format_to(context.out(), "{}", to_string(str));
}
};
The same thing shall apply for wide strings.
Note: I do not have the time to participate in standardisation in order to open formal proposals, but I wish someöne else does based on my ideas.
Regards,
Jaiganésh Kumaran.
Received on 2024-03-15 22:54:57