{"id":104795,"date":"2021-02-04T07:00:00","date_gmt":"2021-02-04T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=104795"},"modified":"2021-02-03T06:57:17","modified_gmt":"2021-02-03T14:57:17","slug":"2021024-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20210204-00\/?p=104795","title":{"rendered":"Usage patterns for winrt::unbox_value_or"},"content":{"rendered":"<p>C++\/WinRT&#8217;s <code>unbox_value_or<\/code> template function takes an <code>IInspectable<\/code> (which represents an arbitrary object) and tries to unbox it to a specified target type. If unable, it returns a provided default value.<\/p>\n<p>The most explicit way of using it is to specialize the type, and pass a default value of that same type.<\/p>\n<pre>auto value = unbox_value_or&lt;Thing&gt;(obj, Thing{ params });\r\n<\/pre>\n<p>This is basically the belt-and-suspenders method. You make sure the output type is a <code>Thing<\/code> by passing it as an explicit template type parameter, and you provide the default value as a <code>Thing<\/code>. There&#8217;s no possible way anybody could misunderstand you.<\/p>\n<p>But you can go with just the belt or just the suspenders.<\/p>\n<p>If you choose the belt, you can pass the type explicitly and let the compiler infer the default value, assuming that the constructor is not explicit.<\/p>\n<pre>auto value = unbox_value_or&lt;Thing&gt;(obj, { params });\r\n<\/pre>\n<p>If there is a single parameter, you can omit the braces, again assuming the constructor is not explicit.<\/p>\n<pre>auto value = unbox_value_or&lt;Thing&gt;(obj, param);\r\n<\/pre>\n<p>Or you can go with the suspenders and pass the default value explicitly and let the compiler infer the type.<\/p>\n<pre>auto value = unbox_value_or(obj, Thing{ params });\r\n<\/pre>\n<p>In the special case that the thing you are trying to unbox is a string, the second parameter goes through <code>param::hstring<\/code> which will use string references where possible, thereby avoiding allocating memory for an actual string unless it turns out to be necessary. (In the other cases, you pass a constructed default object, even if it turns out never to be used.)<\/p>\n<p>Of course, if you omit both the explicit template type parameter and an explicit type for the default value, then your pants fall down.<\/p>\n<pre>\/\/ Compiler can't deduce what you're trying to do.\r\nauto value = unbox_value_or(obj, { params });\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Two templates, many patterns.<\/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-104795","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Two templates, many patterns.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104795","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=104795"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/104795\/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=104795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=104795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=104795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}