C++/WinRT provides a pair of functions for converting between UTF-8 strings (with code units represented as char) and UTF-16 strings (code units of wchar_t).
The to_string function takes a std::wstring_view of UTF-16 code units and converts them to a UTF-8 string, represented as a std::string.
Conversely, the to_hstring function takes a std::string_view of UTF-8 code units and converts them to a UTF-16 string, represented as a winrt::hstring.
The argument to to_string and to_hstring can be anything convertible to the corresponding string view type. Types that fall into this category include
| Type | Converts to |
|---|---|
std::string |
std::string_view |
std::wstring |
std::wstring_view |
winrt::hstring |
std::wstring_view |
We’ll put these conversions to good use next time.
should there also be a to_u16string? and versions that take a u8string_view?
I suppose I get why the wstrings are named like that (string for wide characters) but I’m not sure what to think of hstring (high characters??). You explained a while ago that the H in HRESULT likely means ‘handle’, but I’m not sure that makes sense here.. Any clues? 🙂
Yeah, I’m pretty sure it’s a “handle” to a string, in some logical sense.
See also Raymond’s Complete Guide to HSTRING: https://devblogs.microsoft.com/oldnewthing/20160615-00/?p=93675
The documentation says that winrt::hstring encapsulates HSTRING, and that HSTRING is a handle to a Windows Runtime string, so I think your guess is correct. The documentation for HSTRING adds “use HSTRING to represent immutable strings in the Windows Runtime” and there are various associate API functions, e.g., WindowsCreateString and WindowsDeleteString.