fmtlib / fmt

A modern formatting library
https://fmt.dev
Other
20.56k stars 2.46k forks source link

Question: what's the difference between string-like formatters and where are they defined? #3942

Closed tocic closed 5 months ago

tocic commented 5 months ago

It's not clear for me from the docs if this test project is correct. What are the differences between fmt::formatter<const char*>, fmt::formatter<std::string_view>, fmt::formatter<std::string>? Can I include only fmt/core.h in headers (as in the second example here)? I thought the CE in fmt 10.2.1 (not on trunk) was related to this issue, but now I'm not sure.

vitaut commented 5 months ago

Those are specializations for different string types. I will investigate why your example doesn't work with std::string formatter but in the meantime you can use a formatter for fmt::string_view which works: https://godbolt.org/z/eo4Y7d7nP.

vitaut commented 5 months ago

Documented the list of formatter specializations provided by fmt/base.h (which will superseed fmt/core.h): https://github.com/fmtlib/fmt/commit/f30f1fd5141dd22afdcd32fafaa1f58f10bae8fc. In general it should be sufficient to use fmt::string_view but if you want a formatter of std::string for some reason you should include fmt/format.h.