-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Standardisation proposal feedback #518
Comments
Performance impact looks negligible, even on the
There is a slight increase in binary code due to passing size around but this can be mitigated with extra overloads if there is interest (probably only relevant for embedded). |
The
See also http://zverovich.net/2017/08/20/format-api-improvements.html. Reordering of specifiers doesn't seem like a good idea. For example, is |
Internationalization seems out of scope. Marking as complete (won't fix) as well. |
Design review comments: https://issues.isocpp.org/show_bug.cgi?id=322 |
Added section "Compile-time processing of format strings" http://fmtlib.net/Text%20Formatting.html#CompileTimeFormat. |
Need to document parse context and explain how the new extension API can be used to implement constexpr checks for user-defined types. |
As discussed with Howard Hinnant, added a note regarding integration with D0355 "Extending to Calendars and Time Zones" to the Extensibility section |
Documented |
Added a note about named arguments in fmtlib/fmt.dev@cc4cc49. |
|
Need to rename "numeric" to "arithmetic" because this is the term used in the standard. |
Preliminary work to support iterators: c095445 |
As of 1029119 the library supports |
Output iterators and pre-computation of output size (with
|
Re locales: most formatting is locale-independent but for |
Added a benchmark: http://fmtlib.net/Text%20Formatting.html#Benchmarks Would be good to show the reduction in code bloat due to |
Added an appendix comparing binary code sizes: http://fmtlib.net/Text%20Formatting.html#BinaryCode |
There seems to be a strong preference for having Most names are already unambiguous but need to double check. |
Added |
Renamed |
Dropped nested namespace template <class charT>
class basic_parse_context;
template <class OutputIterator, class charT>
class basic_context;
using parse_context = basic_parse_context<char>;
using context = basic_context<implementation-defined>;
using wcontext = basic_context<implementation-defined>;
template <class Context, class... Args>
format_arg_store<Context, Args...> make_args(const Args&... args);
template <class... Args>
format_arg_store<context, Args...> make_args(const Args&... args); |
Update names: basic_context -> basic_format_context
context -> format_context
wcontext -> wformat_context
make_args -> make_format_args leaving |
Use of ADL instead of specialization is problematic because the namespace ns {
struct S {};
struct format_spec {};
constexpr std::pair<fmt::parse_context::iterator, format_specs>
parse(fmt::type<S>, fmt::parse_context& ctx) { ... }
template <typename FormatContext>
auto format(const format_specs& spec, const S&, FormatContext& ctx) { ... }
} vs namespace ns {
struct S {};
}
namespace fmt {
template <>
struct formatter<ns::S> {
constexpr auto parse(parse_context& ctx) { ... }
template <typename FormatContext>
auto format(const ns::S&, FormatContext& ctx) { ... }
};
} However, this will complicate the API because |
Creating a formatting object from format specification string should be easy now: formatter<T> f;
parse_context ctx(fmt);
f.parse(ctx); |
The header |
@vitaut - I'm having some trouble understanding why the ADL support requires such a large change to the parse API. If I'm understanding the fmlib internals correctly, then I would imagine that you could instead just use ADL to find/construct the correct formatter instance and leave the formatter interface itself completely untouched. Replace
Example: https://gcc.godbolt.org/z/IGxGva (obviously would need a little bit extra to deal with That is roughly the kind of approach I took with formatxx. Instead of constructing a formatter type I instead take a function pointer, but the idea is the same: a wrapper uses ADL to find the right function pointer which is later used to do the actual formatting work. |
That's a great idea, thanks a lot! This is indeed much better than having separate |
Added a visitation API example in fmtlib/fmt.dev@3a663df. |
With compile-time checks, it will be possible to guarantee that formatting functions don't throw (if the underlying iterator operations don't throw), so providing a global error handler is unnecessary. |
Disallowed leading zeros in cases other than |
Reopened the ADL question per additional feedback from LEWG and others. Problems with ADL:
|
Cannot use |
Escaping (incl. for built-in types) can be done by providing a custom formatter for a wrapper type. |
Removed |
I just wanted to give a big +1 to the locale section of the proposal. Locale-independent floating point formatting is important for text-based interchange formats, and it's a real pain at the moment. Thank you for your efforts on this process. |
Thanks for the feedback, @cgmb. |
Clarified when |
Parameterizing formatting functions is not a good idea because (1) we don't want multiple instantiations of type-erased functions and (2) we want implicit conversions into |
Lars is working on a paper that proposes formatters for more standard types, so marking this as fixed. |
Closing issues addressed by https://isocpp.org/files/papers/P1652R0.html |
I'll capture standardization proposal comments here, then prioritize and address them.
Use of
basic_cstring_view
.Expressed by: Lee Howes, Nicol Bolas, Bengt Gustafsson, Thiago Macieira
Pros: less code bloat if already have a null-terminated string
Cons: overhead for non-null-terminated strings
Alternatives:
basic_string_view
const char*
andbasic_string_view
.Compile-time format string processing
Expressed by: LEWG
Named arguments.
Expressed by: Bengt Gustafsson
Joël Lamotte: mention in a section about potential extensions
Internationalization (translation) facilities.
Expressed by: Bengt Gustafsson
This seems out of scope of the library, but can be built on top of it.
Make passing a locale easier. Currently it requires creating a buffer.
Expressed by: Bengt Gustafsson
Reordering of format specifiers.
Expressed by: Bengt Gustafsson
May introduce ambiguity in the grammar.
Pass a range (
string_view
) instead of a cursor toformat_value
.Expressed by: Bengt Gustafsson
Make creating a formatting object from format specification string easy.
Expressed by: Bengt Gustafsson, LEWG
Experimental branch: https://github.com/fmtlib/fmt/tree/ext
Option 1 - separate class:
Option 2 - lambda:
Add a way to define a global error handler for formatting errors (where currently exceptions are thrown) to make it possible, for example, to "ignore the error, default to default format (corresponding to {}), and log the error to the non-critical error log".
Expressed by: Sergey Ignatchenko
Refer to d0355r4 and mention integration possibilities. According to Howard Hinnant there should be no problem plugging P0355 into P0645.
Make sure impl can pre-compute output size.
Expressed by: LEWG
Look at using or explain why not to use an output iterator.
Expressed by: LEWG
Overloads of formatting functions for wide strings.
Expressed by: Howard Hinnant.
Benchmark results.
Expressed by: LEWG
Consider using floating-point format with a round-trip guarantee (as the one described in P0067) as default.
Expressed by: [fr_dav], Sean Parent (https://www.reddit.com/r/cpp/comments/81ta6q/the_fmt_formatting_library_is_now_available_in/dv7gpj9/), http://disq.us/p/1r3a4tt
Consider using standalone functions found by ADL instead of specialized struct template for the extension API: https://www.reddit.com/r/cpp/comments/856p3z/text_formatting_at_the_iso_c_standards_meeting_in/dvwdxyf/
Expressed by: matthieum, Eric Niebler, LEWG, Sean Middleditch
Clarify that compile-time checks are possible for user-defined types with custom parsers.
Expressed by: LEWG
Add
to_chars
to benchmark.Expressed by: Nicolai Josuttis
Add
format_to_n
that takes an output iterator and a size / maximum inserted lengthExpressed by: LEWG, Bengt Gustafsson
Reduce use of nested namespaces
Expressed by: Titus Winters
Choose a better name for
count
Expressed by: LEWG
Custom formatting of built-in types, e.g. to escape strings when formatting JavaScript code
Expressed by: Sean Parent (http://disq.us/p/1r3a4tt).
Give a realistic example of the visitation API.
Expressed by: LEWG, Nicolai Josuttis
Rename
visit
to something more specific likevisit_format_arg
Expressed by: Mateusz Pusz.
Disallow consecutive zeros in arg-id (
{000}
).Expressed by: Zhihao Yuan
Specify formatting behavior for arithmetic types using
to_chars
where possible.Expressed by: Zhihao Yuan
Clarify what is the current locale.
Expressed by: Zhihao Yuan
Default floating point formatting should be shortest round-trip format
Expressed by: Zhihao Yuan
Add missing specializations of
formatter
forbasic_string
,basic_string_view
, andchar*
.Expressed by: LEWG
Use
format_args
instead ofmake_*format_args
.Expressed by: Zhihao Yuan
Clarify which specifiers (particularly sign) apply to nan & inf.
Expressed by: Antony Polukhin
Provide formatters for more standard types.
Expressed by: Lars Gullik Bjønnes
Consider adding ostream
operator<<
support.Expressed by: Lars Gullik Bjønnes
Clarify behavior when mixing character and string types
Expressed by: Zhihao Yuan
Consider reducing the number of overloads by parameterizing formatting functions on format string type similarly to
path(Source)
.Expressed by: LWG
Clarify why
is_integral
andis_arithmetic
are needed or remove.Expressed by: Stephan T. Lavavej
Clarify when
formatter::parse
should stop consuming the input (greedy, '}', etc.)Expressed by: Tomasz Kamiński
Allow the use of
#
with hexfloata
format to print leading0x
.Expressed by: Tomasz Kamiński
Consider changing the behavior of
=
to produce "+ 0x6" instead of "+0x 6" onformat("{: =+#8x}", 6)
or remove=
altogether.Expressed by: Tomasz Kamiński, Zhihao Yuan
It should be possible to format integer as char with the
c
format specifier, e.g.format("{:c}", 65)
.Expressed by: Zhihao Yuan
Add a context type with an iterator used by
formatted_size
to be able to do something like:and define
format
functions in a source file.Expressed by: Tomasz Kamiński
Add a specifier for textual formatting of
bool
s, e.g.s
ort
. This is the default but would be nice to have an explicit specifier for consistency.Expressed by: Zhihao Yuan
Consider making the default pointer format not add "0x" prefix unless the
#
specifier is passed.Expressed by: LWG
Add a specifier to format negative zero without '-'.
Expressed by: Alan Talbot
The text was updated successfully, but these errors were encountered: