-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
multiple formats for a single type ? #862
Comments
With fmt 5 you can also have two functions that perform different formatting, e.g. // Alternative you can format into memory_buffer instead of returning a string.
std::string format_xml(int n) {
return fmt::format("<answer>{}</answer>", n);
}
std::string format_debug(int n) {
return fmt::format("answer={}", n);
} |
but what if a user-defined formatter needs different ways to be formatted ? how would you format, e.g. a |
@vitaut this really doesn't work: for instance I cannot provide a custom serialization for std::vector :
because if at some point someone includes <fmt/ranges.h> it won't compile anymore |
(which is sensibly the same problem than #865) |
fmt provides you with formatting primitives that you can use to build your own functions that write data in XML or any other format. You probably shouldn't plug the data-format specific logic into the fmt's formatters themselves in the same way as you wouldn't write a custom ostream class for XML output. The problem with |
So, this is more or less a follow-up of #859 : how can there be multiple distinct ways to format a given type with fmt 5 ?
e.g., I'd like to have in one file a formatter which generates a debug-like output, and in another file a formatter which generates some HTML representation, for a given C++ type
foo
.With fmt 4 this is easy: just have a function
format_html
and anotherformat_debug
which both take afmt::MemoryWriter
and afoo
but serialize the type according to the desired output.The text was updated successfully, but these errors were encountered: