The Windows Runtime winrt::hstring and the C++ std::wstring are inter-assignable

Raymond Chen

For the past few days, I’ve been talking about converting between various string types while avoiding data loss and security issues if an embedded null is present. But in the case where you are dealing with winrt::hstring and std::wstring, the story is much simpler.

The winrt::hstring and std::wstring types can simply be assigned to each other. No need to do funny wstring_view or c_str() nonsense.

winrt::hstring h;
std::wstring s;

h = s; // this works!
s = h; // this also works!

The assignments work because winrt::hstring and std::wstring both support assignment from std::wstring_view, and both winrt::hstring and std::wstring are convertible to std::wstring_view.

1 comment

Leave a comment

  • Neil Rashbrook 0

    So what you’re saying is that two of each of yesterday’s function bodies could have been return s; or return v; as appropriate?

Feedback usabilla icon