mirror of
https://github.com/fmtlib/fmt.git
synced 2025-09-25 22:20:54 +02:00
Update docs
This commit is contained in:
48
doc/api.md
48
doc/api.md
@@ -560,30 +560,36 @@ and string types as well as user-defined types with `format` methods taking
|
|||||||
the format context type as a template parameter in their `formatter`
|
the format context type as a template parameter in their `formatter`
|
||||||
specializations. For example:
|
specializations. For example:
|
||||||
|
|
||||||
template <> struct fmt::formatter<point> {
|
|
||||||
constexpr auto parse(format_parse_context& ctx);
|
|
||||||
|
|
||||||
template <typename FormatContext>
|
|
||||||
auto format(const point& p, FormatContext& ctx) const;
|
|
||||||
};
|
|
||||||
|
|
||||||
Format string compilation can generate more binary code compared to the
|
|
||||||
default API and is only recommended in places where formatting is a
|
|
||||||
performance bottleneck.
|
|
||||||
|
|
||||||
The same API supports formatting at compile time e.g. in `constexpr` functions.
|
|
||||||
It works with built-in and user-defined formatters that have `constexpr` `parse`
|
|
||||||
and `format` methods. Example ([run](https://www.godbolt.org/z/rzY8Tcjf8)):
|
|
||||||
|
|
||||||
struct point {
|
struct point {
|
||||||
double x;
|
double x;
|
||||||
double y;
|
double y;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <> struct fmt::formatter<point> {
|
template <> struct fmt::formatter<point> {
|
||||||
constexpr auto parse(format_parse_context& ctx) {
|
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
|
||||||
return ctx.begin();
|
|
||||||
|
template <typename FormatContext>
|
||||||
|
auto format(const point& p, FormatContext& ctx) const {
|
||||||
|
return format_to(ctx.out(), "({}, {})"_cf, p.x, p.y);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
using namespace fmt::literals;
|
||||||
|
std::string s = fmt::format("{}"_cf, point(4, 2));
|
||||||
|
|
||||||
|
Format string compilation can generate more binary code compared to the
|
||||||
|
default API and is only recommended in places where formatting is a
|
||||||
|
performance bottleneck.
|
||||||
|
|
||||||
|
The same APIs support formatting at compile time e.g. in `constexpr`
|
||||||
|
and `consteval` functions. Additionally there is an experimental
|
||||||
|
`FMT_STATIC_FORMAT` that allows formatting into a string of the exact
|
||||||
|
required size at compile time. Compile-time formatting works with built-in
|
||||||
|
and user-defined formatters that have `constexpr` `format` methods.
|
||||||
|
Example:
|
||||||
|
|
||||||
|
template <> struct fmt::formatter<point> {
|
||||||
|
constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); }
|
||||||
|
|
||||||
template <typename FormatContext>
|
template <typename FormatContext>
|
||||||
constexpr auto format(const point& p, FormatContext& ctx) const {
|
constexpr auto format(const point& p, FormatContext& ctx) const {
|
||||||
@@ -591,12 +597,14 @@ and `format` methods. Example ([run](https://www.godbolt.org/z/rzY8Tcjf8)):
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using namespace fmt::literals;
|
constexpr auto s = FMT_STATIC_FORMAT("{}", point(4, 2));
|
||||||
constexpr std::string s = fmt::format("{}"_cf, point(1, 2));
|
const char* cstr = s.c_str(); // Points the static string "(4, 2)".
|
||||||
|
|
||||||
|
::: operator""_cf
|
||||||
|
|
||||||
::: FMT_COMPILE
|
::: FMT_COMPILE
|
||||||
|
|
||||||
::: operator""_cf
|
::: FMT_STATIC_FORMAT
|
||||||
|
|
||||||
<a id="color-api"></a>
|
<a id="color-api"></a>
|
||||||
## Terminal Colors and Text Styles
|
## Terminal Colors and Text Styles
|
||||||
|
Reference in New Issue
Block a user