mirror of
https://github.com/fmtlib/fmt.git
synced 2025-01-26 12:35:32 +00:00
Minor documentation changes
This commit is contained in:
parent
61fb85618c
commit
4809e2956a
25
doc/api.rst
25
doc/api.rst
@ -58,29 +58,26 @@ formatting::
|
|||||||
The format string syntax is described in the documentation of
|
The format string syntax is described in the documentation of
|
||||||
`strftime <http://en.cppreference.com/w/cpp/chrono/c/strftime>`_.
|
`strftime <http://en.cppreference.com/w/cpp/chrono/c/strftime>`_.
|
||||||
|
|
||||||
Formatting User-Defined types
|
Formatting user-defined types
|
||||||
-----------------------------
|
-----------------------------
|
||||||
|
|
||||||
A custom ``format_arg`` function may be implemented and used to format any user-
|
A custom ``format_arg`` function may be implemented and used to format any
|
||||||
defined type. That is how date and time formatting described in the previous
|
user-defined type. That is how date and time formatting described in the
|
||||||
section is implemented in :file:`fmt/time.h`. The following example shows how to implement custom formatting for a user-defined structure.
|
previous section is implemented in :file:`fmt/time.h`. The following example
|
||||||
|
shows how to implement custom formatting for a user-defined structure.
|
||||||
|
|
||||||
::
|
::
|
||||||
|
|
||||||
struct MyStruct { double a, b, c, d; };
|
struct MyStruct { double a, b; };
|
||||||
|
|
||||||
void format_arg(fmt::BasicFormatter<char> &f,
|
void format_arg(fmt::BasicFormatter<char> &f,
|
||||||
const char *&format_str, const MyStruct &s) {
|
const char *&format_str, const MyStruct &s) {
|
||||||
f.writer().write("[MyStruct: a={:.1f}, b={:.2f}, c={:.3f}, d={:.4f}]",
|
f.writer().write("[MyStruct: a={:.1f}, b={:.2f}]", s.a, s.b);
|
||||||
s.a, s.b, s.c, s.d);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void f()
|
MyStruct m = { 1, 2 };
|
||||||
{
|
std::string s = fmt::format("m={}", n);
|
||||||
MyStruct m = { 1, 2, 3, 4 };
|
// s == "m=[MyStruct: a=1.0, b=2.00]"
|
||||||
std::string s = fmt::format("m={}", n);
|
|
||||||
// s == "m=[MyStruct: a=1.0, b=2.00, c=3.000, d=4.0000]"
|
|
||||||
}
|
|
||||||
|
|
||||||
Note in the example above the ``format_arg`` function ignores the contents of
|
Note in the example above the ``format_arg`` function ignores the contents of
|
||||||
``format_str`` so the type will always be formatted as specified. See
|
``format_str`` so the type will always be formatted as specified. See
|
||||||
|
Loading…
x
Reference in New Issue
Block a user