mirror of
https://github.com/fmtlib/fmt.git
synced 2024-12-24 12:14:26 +00:00
Add an example of reusing formatters
This commit is contained in:
parent
f66ba6508a
commit
60c662b3a7
19
doc/api.rst
19
doc/api.rst
@ -130,6 +130,25 @@ always be formatted in the same way. See ``formatter<tm>::parse`` in
|
|||||||
:file:`fmt/time.h` for an advanced example of how to parse the format string and
|
:file:`fmt/time.h` for an advanced example of how to parse the format string and
|
||||||
customize the formatted output.
|
customize the formatted output.
|
||||||
|
|
||||||
|
You can also reuse existing formatters, for example::
|
||||||
|
|
||||||
|
enum color {red, green, blue};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct fmt::formatter<color>: formatter<string_view> {
|
||||||
|
// parse is inherited from formatter<string_view>.
|
||||||
|
template <typename FormatContext>
|
||||||
|
auto format(color c, FormatContext &ctx) {
|
||||||
|
string_view name = "unknown";
|
||||||
|
switch (c) {
|
||||||
|
case red: name = "red"; break;
|
||||||
|
case green: name = "green"; break;
|
||||||
|
case blue: name = "blue"; break;
|
||||||
|
}
|
||||||
|
return formatter<string_view>::format(name, ctx);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
This section shows how to define a custom format function for a user-defined
|
This section shows how to define a custom format function for a user-defined
|
||||||
type. The next section describes how to get ``fmt`` to use a conventional stream
|
type. The next section describes how to get ``fmt`` to use a conventional stream
|
||||||
output ``operator<<`` when one is defined for a user-defined type.
|
output ``operator<<`` when one is defined for a user-defined type.
|
||||||
|
Loading…
Reference in New Issue
Block a user