{"id":109925,"date":"2024-06-21T07:00:00","date_gmt":"2024-06-21T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109925"},"modified":"2024-06-21T09:09:13","modified_gmt":"2024-06-21T16:09:13","slug":"20240621-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240621-00\/?p=109925","title":{"rendered":"The Windows Runtime <CODE>winrt::<WBR>hstring<\/CODE> and the C++ <CODE>std::<WBR>wstring<\/CODE> are inter-assignable"},"content":{"rendered":"<p>For the past few days, I&#8217;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 <code>winrt::<wbr \/>hstring<\/code> and <code>std::<wbr \/>wstring<\/code>, the story is much simpler.<\/p>\n<p>The <code>winrt::<wbr \/>hstring<\/code> and <code>std::<wbr \/>wstring<\/code> types can simply be assigned to each other. No need to do funny <code>wstring_view<\/code> or <code>c_str()<\/code> nonsense.<\/p>\n<pre>winrt::hstring h;\r\nstd::wstring s;\r\n\r\nh = s; \/\/ this works!\r\ns = h; \/\/ this also works!\r\n<\/pre>\n<p>The assignments work because <code>winrt::<wbr \/>hstring<\/code> and <code>std::<wbr \/>wstring<\/code> both support assignment from <code>std::<wbr \/>wstring_view<\/code>, and both <code>winrt::<wbr \/>hstring<\/code> and <code>std::<wbr \/>wstring<\/code> are convertible to <code>std::<wbr \/>wstring_view<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just assign them over, no cermony necessary.<\/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-109925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Just assign them over, no cermony necessary.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109925","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=109925"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109925\/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=109925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}