However, there are a few issues here:
1. As of now, we do not have std::u8cout or even std::u8ostream. So there is really no easy way to create and use a stream for Unicode characters. So even if I implement
template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const quantity& q)
correctly, we do not have an easy way to use it.
2. In order to implement the above, I could imagine such an interface for a symbol prefix:
template<typename CharT, typename Traits, typename Prefix, typename Ratio>
inline constexpr std::basic_string_view<CharT, Traits> prefix_symbol;
and its partial specializations for different prefixes/ratios:
template<typename CharT, typename Traits>
inline constexpr std::basic_string_view<char, Traits> prefix_symbol<char, Traits, si_prefix, std::micro> = "u";
template<typename CharT, typename Traits>
inline constexpr std::basic_string_view<CharT, Traits> prefix_symbol<CharT, Traits, si_prefix, std::micro> = u8"\u00b5"; // µ
template<typename CharT, typename Traits>
inline constexpr std::basic_string_view<CharT, Traits> prefix_symbol<CharT, Traits, si_prefix, std::milli> = "m";
The problem is that the above code will not compile. Specialization for all `CharT` will not be possible to be initialized with a literal like "m". Also, there is no generic mechanism to initialize all Unicode-based versions of the type with the same literal as each of them requires a different prefix (u8, u, U). Providing a specialization for every character type here is going to be a nightmare for library authors.
To solve the second problem fmt and chrono defined something called STATICALLY-WIDEN (
http://wg21.link/time.general) but it seems that it is more a specification hack rather than the implementation technique. I call it a hack as it currently addresses only `char` and `wchar_t` and does not mention
Unicode characters
at all as of now.
Dear SG16 members, do you have any BKMs or suggestions on how to write a library that is Unicode aware and safe in an easy and approachable way? Should we strive to provide a nice-looking representation of units for outputs that support Unicode (console, files, etc) or should we, as ever before, just support only `char` and `wchar_t` and ignore the existence of Unicode in C++?