mirror of
https://github.com/fmtlib/fmt.git
synced 2025-07-30 02:37:36 +02:00
Update example (#2522)
This commit is contained in:
14
doc/api.rst
14
doc/api.rst
@ -186,7 +186,9 @@ template and implement ``parse`` and ``format`` methods::
|
|||||||
|
|
||||||
#include <fmt/format.h>
|
#include <fmt/format.h>
|
||||||
|
|
||||||
struct point { double x, y; };
|
struct point {
|
||||||
|
double x, y;
|
||||||
|
};
|
||||||
|
|
||||||
template <> struct fmt::formatter<point> {
|
template <> struct fmt::formatter<point> {
|
||||||
// Presentation format: 'f' - fixed, 'e' - exponential.
|
// Presentation format: 'f' - fixed, 'e' - exponential.
|
||||||
@ -210,8 +212,7 @@ template and implement ``parse`` and ``format`` methods::
|
|||||||
if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;
|
if (it != end && (*it == 'f' || *it == 'e')) presentation = *it++;
|
||||||
|
|
||||||
// Check if reached the end of the range:
|
// Check if reached the end of the range:
|
||||||
if (it != end && *it != '}')
|
if (it != end && *it != '}') throw format_error("invalid format");
|
||||||
throw format_error("invalid format");
|
|
||||||
|
|
||||||
// Return an iterator past the end of the parsed range:
|
// Return an iterator past the end of the parsed range:
|
||||||
return it;
|
return it;
|
||||||
@ -222,10 +223,9 @@ template and implement ``parse`` and ``format`` methods::
|
|||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
auto format(const point& p, FormatContext& ctx) -> decltype(ctx.out()) {
|
auto format(const point& p, FormatContext& ctx) -> decltype(ctx.out()) {
|
||||||
// ctx.out() is an output iterator to write to.
|
// ctx.out() is an output iterator to write to.
|
||||||
return format_to(
|
return presentation == 'f'
|
||||||
ctx.out(),
|
? format_to(ctx.out(), "({:.1f}, {:.1f})", p.x, p.y)
|
||||||
presentation == 'f' ? "({:.1f}, {:.1f})" : "({:.1e}, {:.1e})",
|
: format_to(ctx.out(), "({:.1e}, {:.1e})", p.x, p.y);
|
||||||
p.x, p.y);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user