{"id":112803,"date":"2026-10-28T07:00:00","date_gmt":"2026-10-28T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=112803"},"modified":"2026-10-28T12:55:36","modified_gmt":"2026-10-28T19:55:36","slug":"20261028-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20261028-00\/?p=112803\/","title":{"rendered":"C++\/WinRT overcorrection: Using <CODE>make_self<\/CODE> and immediately converting it to the projected interface"},"content":{"rendered":"<p>In C++\/WinRT, there are two ways of creating objects that you yourself implement.<\/p>\n<p>For the purpose of this discussion, let <code>Impl<\/code> be the class you wrote that implements a Windows Runtime object. If you are implementing something that was declared in an IDL file, it will be the <code>winrt::<wbr \/>Namespace::<wbr \/>implementation::<wbr \/>ClassName<\/code> for the instance object or it will be the <code>winrt::<wbr \/>Namespace::<wbr \/>factory_<wbr \/>implementation::<wbr \/>ClassName<\/code> for the factory object. If it wasn&#8217;t declared in an IDL file, then it is the class that you manually derived from <code>winrt::<wbr \/>implements<\/code>.<\/p>\n<p>One way to create the object is to use <code>make&lt;Impl&gt;<\/code>, which creates the implementation object and then returns the Windows Runtime projection version of that object. (If <code>Impl<\/code> is manually-derived from <code>winrt::<wbr \/>implements<\/code>, the projection is the first implemented interface.)<\/p>\n<p>From that Windows Runtime projection, you can regain access to the implementation object by doing <code>get_self&lt;Impl&gt;(projected)<\/code>, if you want to access things in the implementation object that aren&#8217;t exposed in the projection.<\/p>\n<p>Alternatively, you use <code>make_self&lt;Impl&gt;<\/code>, which creates the object but returns a <code>winrt::<wbr \/>com_ptr&lt;Impl&gt;<\/code> instead of a projection. You can then access things in the implementation object from that <code>winrt::<wbr \/>com_ptr<\/code>, and convert it to the projected type with the <code>*<\/code> operator.<\/p>\n<table style=\"border-collapse: collapse; text-align: center;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td><tt>make<\/tt><\/td>\n<td>&nbsp;<\/td>\n<td><tt>make_self<\/tt><\/td>\n<\/tr>\n<tr>\n<td>\u2193<\/td>\n<td>&nbsp;<\/td>\n<td>\u2193<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px currentcolor;\" rowspan=\"2\">projection<\/td>\n<td><tt>get_self<\/tt><br \/>\n\u2192<\/td>\n<td style=\"border: solid 1px currentcolor;\" rowspan=\"2\">implementation<\/td>\n<\/tr>\n<tr>\n<td>\u2190<br \/>\n<tt>operator*<\/tt><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Which one you choose depends on where you want to go first.<\/p>\n<p>If you are first going to access <a title=\"One way of thinking about access specifiers in C++\/WinRT implementation types\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20261009-00\/?p=112764\"> non-projected public members of the implementation class<\/a>, use the <code>-&gt;<\/code> operator on the <code>com_ptr&lt;Impl&gt;<\/code>. For example, you might want to set up the object before returning the projection.<\/p>\n<pre>winrt::IVector&lt;winrt::Widget&gt;\r\n    MakeWidgets(int style, int count)\r\n{\r\n    std::vector&lt;winrt::Widget&gt; v(count, nullptr);\r\n    std::generate(v.begin(), v.end(), [&amp;] {\r\n        <span style=\"border: solid 1px currentcolor; border-bottom: none;\">auto widget = winrt::make_self&lt;implementation::Widget&gt;(style);<\/span>\r\n        <span style=\"border: solid 1px currentcolor; border-top: none;\">widget-&gt;Enabled(true);                                        <\/span>\r\n        return *widget;\r\n    });\r\n    return winrt::multi_threaded_vector(std::move(v));\r\n}\r\n<\/pre>\n<p>But if you are just going to use the projection, you can take the direct route instead of going the long way.<\/p>\n<pre>\/\/ The long way\r\n\r\nwinrt::IVector&lt;winrt::Widget&gt;\r\n    MakeWidgets(int style, int count)\r\n{\r\n    std::vector&lt;winrt::Widget&gt; v(count, nullptr);\r\n    std::generate(v.begin(), v.end(), [&amp;] {\r\n        <span style=\"border: solid 1px currentcolor;\">auto widget = winrt::make_self&lt;implementation::Widget&gt;(style);<\/span>\r\n        return *widget;\r\n    });\r\n    return winrt::multi_threaded_vector(std::move(v));\r\n}\r\n<\/pre>\n<table style=\"border-collapse: collapse; text-align: center;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td><tt>make_self<\/tt><\/td>\n<\/tr>\n<tr>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td>\u2193<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px currentcolor;\">projection<\/td>\n<td>\u2190<br \/>\n<tt>operator*<\/tt><\/td>\n<td style=\"border: solid 1px currentcolor;\">implementation<\/td>\n<\/tr>\n<tr>\n<td>\u2193<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<tr>\n<td>returned<\/td>\n<td>&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>In this case, you don&#8217;t need to pass through the implementation type.<\/p>\n<pre>\/\/ The short way\r\n\r\nwinrt::IVector&lt;winrt::Widget&gt;\r\n    MakeWidgets(int style, int count)\r\n{\r\n    std::vector&lt;winrt::Widget&gt; v(count, nullptr);\r\n    std::generate(v.begin(), v.end(), [&amp;] {\r\n        return winrt::make_self&lt;implementation::Widget&gt;(style);\r\n    });\r\n    return winrt::multi_threaded_vector(std::move(v));\r\n}\r\n<\/pre>\n<table style=\"border-collapse: collapse; text-align: center;\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\">\n<tbody>\n<tr>\n<td><tt>make<\/tt><\/td>\n<\/tr>\n<tr>\n<td>\u2193<\/td>\n<\/tr>\n<tr>\n<td style=\"border: solid 1px currentcolor;\">projection<\/td>\n<\/tr>\n<tr>\n<td>\u2193<\/td>\n<\/tr>\n<tr>\n<td>return<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>My guess is that the people who go the long way don&#8217;t realize that there are two ways. They learned only one way and just rely on it even if there&#8217;s a shorter way.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You are taking the scenic route.<\/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-112803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You are taking the scenic route.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112803","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=112803"}],"version-history":[{"count":1,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112803\/revisions"}],"predecessor-version":[{"id":112804,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/112803\/revisions\/112804"}],"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=112803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=112803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=112803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}