From 20e0d6d8dda8b6a93bf9960dde3278ffbd7ae689 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Mon, 1 Sep 2025 10:15:54 -0700 Subject: [PATCH] Update docs --- doc/api.md | 4 ++-- include/fmt/compile.h | 32 ++++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/doc/api.md b/doc/api.md index 090b57e2..2bc5b0d1 100644 --- a/doc/api.md +++ b/doc/api.md @@ -558,7 +558,7 @@ marked with `FMT_COMPILE` or `_cf` are parsed, checked and converted into efficient formatting code at compile-time. This supports arguments of built-in and string types as well as user-defined types with `format` methods taking the format context type as a template parameter in their `formatter` -specializations. For example: +specializations. For example ([run](https://www.godbolt.org/z/3c13erEoq)): struct point { double x; @@ -586,7 +586,7 @@ 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 ([run](https://www.godbolt.org/z/3c13erEoq)): +Example: template <> struct fmt::formatter { constexpr auto parse(format_parse_context& ctx) { return ctx.begin(); } diff --git a/include/fmt/compile.h b/include/fmt/compile.h index 3ffa8152..cb9f266e 100644 --- a/include/fmt/compile.h +++ b/include/fmt/compile.h @@ -22,8 +22,6 @@ FMT_EXPORT class compiled_string {}; template struct is_compiled_string : std::is_base_of {}; -namespace detail { - /** * Converts a string literal `s` into a format string that will be parsed at * compile time and converted into efficient formatting code. Requires C++17 @@ -41,6 +39,28 @@ namespace detail { # define FMT_COMPILE(s) FMT_STRING(s) #endif +/** + * Converts a string literal into a format string that will be parsed at + * compile time and converted into efficient formatting code. Requires support + * for class types in constant template parameters (a C++20 feature). + * + * **Example**: + * + * // Converts 42 into std::string using the most efficient method and no + * // runtime format string processing. + * using namespace fmt::literals; + * std::string s = fmt::format("{}"_cf, 42); + */ +#if FMT_USE_NONTYPE_TEMPLATE_ARGS +inline namespace literals { +template constexpr auto operator""_cf() { + return FMT_COMPILE(Str.data); +} +} // namespace literals +#endif + +namespace detail { + template constexpr auto first(const T& value, const Tail&...) -> const T& { return value; @@ -559,14 +579,6 @@ template class static_format_result { fmt::formatted_size(FMT_COMPILE(fmt_str), __VA_ARGS__) + 1>( \ FMT_COMPILE(fmt_str), __VA_ARGS__) -#if FMT_USE_NONTYPE_TEMPLATE_ARGS -inline namespace literals { -template constexpr auto operator""_cf() { - return FMT_COMPILE(Str.data); -} -} // namespace literals -#endif - FMT_END_EXPORT FMT_END_NAMESPACE