fmtlib / fmt

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

fmt won't let me specialize a formatter to format mixed character types. #3947

Closed BenFrantzDale closed 2 months ago

BenFrantzDale commented 2 months ago

My use case is migrating a Qt codebase to more-standard C++, so I have a mix of QString (UTF-16) and std::string. I'd like to specialize a formatter that lets me format QString to produce UTF-8, so I can just std::string s = fmt::format("foo: {}", someString); in generic code and have it work if someString is QString or std::string or std::string_view.

AFAICT, fmt's formatter selection is pretty aggressive: if is_std_string_like<T>::value it insists on going with a default formatter, even if I want to specialize it for formatting to a different character type: https://godbolt.org/z/T3fhKrEde

Is there any way around this?

vitaut commented 2 months ago

Are you sure that is_std_string_like<QString>::value returns true? AFAICS QString doesn't have find_first_of (https://doc.qt.io/qt-6/qstring.html) which is used as a std::string[_view] detection heuristic.

BenFrantzDale commented 2 months ago

You seem to be correct, at least in trunk it works: https://godbolt.org/z/ME1j55zMz