C++/WinRT nasty gotcha: winrt::param::hstring constructed from std::wstring_view requires null termination

Raymond Chen

The std::string_view (and wide buddy std::wstring_view) represent a contiguous sequence of characters, not necessarily null-terminated.

One nasty gotcha in C++/WinRT is that if you try to construct a winrt::hstring from a std::wstring_view, the C++/WinRT library requires that your std::wstring_view be followed with a null terminator. If the character one past the end of the view is not a null character, then the C++/WinRT library terminates the program.

I’m not sure if std::wstring_view was the correct data type for representing “a range of characters, followed by a null terminator”, but it’s what C++/WinRT chose, and we’re stuck with it. But at least now you know the bonus null terminator requirement.