{"id":105598,"date":"2021-08-23T07:00:00","date_gmt":"2021-08-23T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=105598"},"modified":"2021-08-22T19:00:46","modified_gmt":"2021-08-23T02:00:46","slug":"20210823-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210823-00\/?p=105598","title":{"rendered":"C++\/WinRT nasty gotcha: <CODE>winrt::param::hstring<\/CODE> constructed from <CODE>std::wstring_view<\/CODE> requires null termination"},"content":{"rendered":"<p>The <code>std::string_view<\/code> (and wide buddy <code>std::wstring_view<\/code>) represent a contiguous sequence of characters, <a href=\"http:\/\/www.open-std.org\/jtc1\/sc22\/wg21\/docs\/papers\/2014\/n3921.html#null-termination\"> not necessarily null-terminated<\/a>.<\/p>\n<p>One nasty gotcha in C++\/WinRT is that if you try to construct a <code>winrt::hstring<\/code> from a <code>std::wstring_view<\/code>, the C++\/WinRT library requires that your <code>std::wstring_view<\/code> 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.<\/p>\n<p>I&#8217;m not sure if <code>std::wstring_view<\/code> was the correct data type for representing &#8220;a range of characters, followed by a null terminator&#8221;, but it&#8217;s what C++\/WinRT chose, and we&#8217;re stuck with it. But at least now you know the bonus null terminator requirement.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>An additional constraint not part of the rules.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-105598","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>An additional constraint not part of the rules.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=105598"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/105598\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=105598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=105598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=105598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}