{"id":109529,"date":"2024-03-13T07:00:00","date_gmt":"2024-03-13T14:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=109529"},"modified":"2024-03-13T10:43:01","modified_gmt":"2024-03-13T17:43:01","slug":"20240313-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240313-00\/?p=109529\/","title":{"rendered":"How well does MFC <CODE>IPTR<\/CODE>\/<CODE>CIP<\/CODE> support class template argument deduction (CTAD)?"},"content":{"rendered":"<p>Continuing our investigation of <a title=\"Class template argument deduction (CTAD) and C++ COM wrappers, part 1: Initial explorations\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240311-00\/?p=109521\"> which C++ COM wrappers support class template argument deduction<\/a> (CTAD), next up is MFC&#8217;s <code>IPTR<\/code>\/<code>CIP<\/code>.<\/p>\n<p>The <code>CIP<\/code> template class fails CTAD because of these constructors:<\/p>\n<pre>template&lt;class _Interface, const IID* _IID&gt;\r\nclass CIP : public _CIP&lt;_Interface, _IID&gt;\r\n{\r\npublic:\r\n    typedef _CIP&lt;_Interface, _IID&gt; BC;\r\n    typedef _Interface Interface;\r\n\r\n    CIP(Interface* pInterface); \/\/ this one\r\n    CIP(Interface* pInterface, BOOL bAddRef); \/\/ and this one\r\n\r\n    \u27e6 other stuff \u27e7\r\n};\r\n<\/pre>\n<p>In this case, the compiler can deduce the <code>_Interface<\/code> template parameter, but it doesn&#8217;t know what <code>_IID<\/code> to use. Again, adding some deduction guides can fix that by saying that it should use the <code>__uuidof(T)<\/code> as the <code>_IID<\/code>.<\/p>\n<pre>template&lt;typename T&gt; CIP(T*)       -&gt; CIP&lt;T, &amp;__uuidof(T)&gt;;\r\ntemplate&lt;typename T&gt; CIP(T*, BOOL) -&gt; CIP&lt;T, &amp;__uuidof(T)&gt;;\r\n<\/pre>\n<p><a title=\"How well does _com_ptr_t support class template argument deduction (CTAD)?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240312-00\/?p=109526\"> As noted before<\/a>, injecting your own deduction guides into somebody else&#8217;s types is not recommended because it may conflict with deduction guides added by the type owner in the future.<\/p>\n<p>Fortunately, in the case of MFC <code>IPTR<\/code>\/<code>CIP<\/code>, you rarely use <code>CIP<\/code> directly. Instead, you use the <code>IPTR<\/code> that was generated from the <code>CIP<\/code>, and that type already knows what interface it is for, so there&#8217;s no need for CTAD.<\/p>\n<pre>using TestPtr = IPTR(Test);\r\n\r\nvoid sample(Test* p)\r\n{\r\n    auto smart = TestPtr(p);\r\n}\r\n<\/pre>\n<p>Okay, but what if you still want to create an MFC smart pointer from a raw pointer without having to specify the interface?<\/p>\n<p>You can do what C++ did before CTAD was invented: &#8220;You kids with your CTADs and deduction guides. In my day, we had to use maker template functions,\u00b9 and we liked it!&#8221;<\/p>\n<pre>template&lt;typename T&gt;\r\nCIP&lt;T, &amp;__uuidof(T)&gt; make_CIP(T* p)\r\n{\r\n    return p;\r\n}\r\n\r\ntemplate&lt;typename T&gt;\r\nCIP&lt;T, &amp;__uuidof(T)&gt; make_CIP(T* p, BOOL bAddRef)\r\n{\r\n    return { p, bAddRef };\r\n}\r\n\r\nvoid sample(Test* p)\r\n{\r\n    auto smart1 = make_CIP(p);\r\n    auto smart2 = make_CIP(p, FALSE);\r\n}\r\n<\/pre>\n<p>This trick also works for <code>_com_ptr_t<\/code>, which is <a title=\"How well does _com_ptr_t support class template argument deduction (CTAD)?\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20240312-00\/?p=109526\"> something I teased last time<\/a>.<\/p>\n<pre>template&lt;typename T&gt;\r\n_com_ptr_t&lt;_com_IIID&lt;T, &amp;__uuidof(T)&gt;&gt;\r\n    make_com_ptr_t(T* p)\r\n{\r\n    return p;\r\n}\r\n\r\nvoid sample(Test* p)\r\n{\r\n    auto smart = make_com_ptr_t(p);\r\n}\r\n<\/pre>\n<p>\u00b9 There are maker template functions all over the C++ standard library. <code>std::<wbr \/>back_<wbr \/>inserter<\/code>, <code>std::<wbr \/>make_<wbr \/>reverse_<wbr \/>iterator<\/code>, <code>std::<wbr \/>make_<wbr \/>pair<\/code>, <i>etc<\/i>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Not very well, thanks to requiring the interface ID to be specified explicitly.<\/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-109529","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Not very well, thanks to requiring the interface ID to be specified explicitly.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109529","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=109529"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/109529\/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=109529"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=109529"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=109529"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}