{"id":109250,"date":"2024-01-08T07:00:00","date_gmt":"2024-01-08T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109250"},"modified":"2024-01-08T09:29:14","modified_gmt":"2024-01-08T17:29:14","slug":"20240108-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240108-00\/?p=109250","title":{"rendered":"How do I prevent my C++\/WinRT implementation class from participating in COM aggregation?"},"content":{"rendered":"<p>In the Windows Runtime there are these things which are called &#8220;composable classes&#8221;. Composable classes allow the Windows Runtime to expose classes which applications can derive from, or at least do something that <i>looks like<\/i> class derivation, even though under the covers it&#8217;s COM aggregation.<\/p>\n<p>In C++\/WinRT, you can give your implementation class the <code>winrt::composable<\/code> tag to make it composable. And if you apply the <code>unsealed<\/code> keyword to your <code>runtimeclass<\/code> in the IDL file, then the C++\/WinRT autogenerated implementation templates will specify <code>winrt::composable<\/code> automatically.<\/p>\n<p>But what if you don&#8217;t want to be composable? How can you force a build break if somebody tries to enable composability?<\/p>\n<p>From looking at the C++\/WinRT code, it appears that you can detect whether the class is composable by snooping at the <code>outer()<\/code> method. <a href=\"https:\/\/github.com\/microsoft\/cppwinrt\/blob\/9b453cfc518bdaa7e2ff590526c4883457fd6065\/strings\/base_implements.h#L804\"> For non-composing classes<\/a>, <code>outer()<\/code> is defined as<\/p>\n<pre>static constexpr inspectable_abi* outer() noexcept { return nullptr; }\r\n<\/pre>\n<p>Whereas <a href=\"https:\/\/github.com\/microsoft\/cppwinrt\/blob\/9b453cfc518bdaa7e2ff590526c4883457fd6065\/strings\/base_implements.h#L814\"> for composing classes<\/a>, <code>outer()<\/code> is defined as<\/p>\n<pre>inspectable_abi* outer() noexcept { return m_outer; }\r\n<\/pre>\n<p>Therefore, we can distinguish them by seeing whether <code>outer()<\/code> is a constexpr nullptr.<\/p>\n<pre>struct Widget : WidgetT&lt;Widget&gt;\r\n{\r\n  Widget()\r\n  {\r\n    <span style=\"border: solid 1px currentcolor; border-bottom: none;\">static_assert(!outer(),                         <\/span>\r\n    <span style=\"border: solid 1px currentcolor; border-top: none;\">              \"Widget must not be composable.\");<\/span>\r\n  }\r\n\r\n  \u27e6 ... \u27e7\r\n};\r\n<\/pre>\n<p>If we are not composing, then <code>outer()<\/code> is a constexpr function that indeed returns <code>nullptr<\/code>, so everything passes.<\/p>\n<p>If we are composing, then <code>outer()<\/code> is a non-constexpr function, and the <code>static_assert<\/code> fails because you are trying to assert a runtime expression at compile time.<\/p>\n<p>Unfortunately, the error message doesn&#8217;t use the static assertion text, because the problem occurred even before the compiler could decide whether the expression passed the assertion.<\/p>\n<pre>error C2131: expression did not evaluate to a constant\r\n<\/pre>\n<p>But at least the line number points to the <code>static_assert<\/code>, so the developer will be taken to your assertion string.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Looking for a clue.<\/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-109250","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Looking for a clue.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109250","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=109250"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109250\/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=109250"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109250"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109250"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}