With printf
I can do something like this
wprintf(L"%hs", "abc");
where the format
is wchar_t * but argument is char *. Is this doable with std::format()
, or do I have to convert one to the other prior?
When I do
std::format(L"{:hs}", "abc");
I get compile error about type mismatch.
As per the documentation, the format string matches Python’s format specification, which does not have any magic for directly dealing with different string types or encodings. (Python does everything behind the scenes with CESU-8, IIRC.)
This is one of those things where I wish the C++ Standard would just Do The Right Thing™, but as the C++ Standards Committee still hasn’t hashed out how they want to handle string encoding transformations (as of Jan 2022) we are stuck with a dumb std::format
.
The ultimate goal, I think (and hope), is that std::format
will one day accept different string types and transform them properly for you without having to change the fmt
argument. For now, however, you must perform the transform yourself.
Alas, ATM std::format
will only accept like string types for all arguments, where the base character type is either char
or wchar_t
. That’s it. (More docs.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With