<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	
	>
<channel>
	<title>
	Comments on: What the Curiously Recurring Template Pattern can bring to your code	</title>
	<atom:link href="https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/</link>
	<description>Jonathan Boccara&#039;s blog</description>
	<lastBuildDate>Sun, 09 Dec 2018 05:32:00 +0000</lastBuildDate>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.5.3</generator>
	<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1525</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 09 Dec 2018 05:32:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1525</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1517&quot;&gt;Stauros Marinis&lt;/a&gt;.

Ok I see! Great point.

If I understand well, the problem happens when the method has the same name in the base and derived class. Is it a normal use case for the CRTP? I need to think about it. In general it is advised to avoid identical names for non-virtual functions across a class hierarchy (see Effective C++ item 36), but does that guideline hold for the CRTP?

When I run the code you provided with the #error I got an compile error even when the function &lt;em&gt;was&lt;/em&gt; defined. Maybe I got your snippet wrong because the comment was formatted. Would you have a snippet in a online compiler such as &lt;a href=&quot;http://coliru.stacked-crooked.com&quot; rel=&quot;nofollow&quot;&gt;coliru&lt;/a&gt; to make sure I get this right? Also, did you consider using a static_assert?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1517">Stauros Marinis</a>.</p>
<p>Ok I see! Great point.</p>
<p>If I understand well, the problem happens when the method has the same name in the base and derived class. Is it a normal use case for the CRTP? I need to think about it. In general it is advised to avoid identical names for non-virtual functions across a class hierarchy (see Effective C++ item 36), but does that guideline hold for the CRTP?</p>
<p>When I run the code you provided with the #error I got an compile error even when the function <em>was</em> defined. Maybe I got your snippet wrong because the comment was formatted. Would you have a snippet in a online compiler such as <a href="http://coliru.stacked-crooked.com" rel="nofollow">coliru</a> to make sure I get this right? Also, did you consider using a static_assert?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Stauros Marinis		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1517</link>

		<dc:creator><![CDATA[Stauros Marinis]]></dc:creator>
		<pubDate>Thu, 06 Dec 2018 13:39:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1517</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1374&quot;&gt;Jonathan Boccara&lt;/a&gt;.

Hello Jonathan! Many thanks for your reply. Please find a snippet below leading to segfault:

#include 
using namespace std;

template  class Base {
public:
    // Build interface - compile time
    void test() {
        D&#038; derv = static_cast(*this);
        derv.test();
    }
};

class Derived : public Base {
public:
    //Implement test; // We forgot to add the function
    /*void test() { cout&#060;&#060;&#034;Conc test&#034;&#060;&#060;endl;}*/
};

// Implement client
template  void client(Base a) {
    a.test();
}

int main()
{
   Derived d;
   client(d);
   return 0;
}

Playing around with a compile time assertion though could work i.e. modifying Base along the  following lines throws an error (generally the logic could be wrapped inside a template for each function):

template  class Base {
public:

/* Safety when using CRTP - enforce method implementation in compile time */
using F = void (D::*)();
F d1 = &#038;D::test;

//Nullptr Check
#if (d1==0) 
  #error &quot;test function not defined on Derived class&quot;
#endif

    // Build interface - compile time
    void test() {
        D&#038; derv = static_cast(*this);
        derv.test();
    }
};

Many thanks ! Stavros]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1374">Jonathan Boccara</a>.</p>
<p>Hello Jonathan! Many thanks for your reply. Please find a snippet below leading to segfault:</p>
<p>#include<br />
using namespace std;</p>
<p>template  class Base {<br />
public:<br />
    // Build interface &#8211; compile time<br />
    void test() {<br />
        D&amp; derv = static_cast(*this);<br />
        derv.test();<br />
    }<br />
};</p>
<p>class Derived : public Base {<br />
public:<br />
    //Implement test; // We forgot to add the function<br />
    /*void test() { cout&lt;&lt;&quot;Conc test&quot;&lt;&lt;endl;}*/<br />
};</p>
<p>// Implement client<br />
template  void client(Base a) {<br />
    a.test();<br />
}</p>
<p>int main()<br />
{<br />
   Derived d;<br />
   client(d);<br />
   return 0;<br />
}</p>
<p>Playing around with a compile time assertion though could work i.e. modifying Base along the  following lines throws an error (generally the logic could be wrapped inside a template for each function):</p>
<p>template  class Base {<br />
public:</p>
<p>/* Safety when using CRTP &#8211; enforce method implementation in compile time */<br />
using F = void (D::*)();<br />
F d1 = &amp;D::test;</p>
<p>//Nullptr Check<br />
#if (d1==0)<br />
  #error &#8220;test function not defined on Derived class&#8221;<br />
#endif</p>
<p>    // Build interface &#8211; compile time<br />
    void test() {<br />
        D&amp; derv = static_cast(*this);<br />
        derv.test();<br />
    }<br />
};</p>
<p>Many thanks ! Stavros</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Guillermo Blanco Amaro		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1391</link>

		<dc:creator><![CDATA[Guillermo Blanco Amaro]]></dc:creator>
		<pubDate>Sun, 14 Oct 2018 12:50:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1391</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1390&quot;&gt;Jonathan Boccara&lt;/a&gt;.

Thank you!]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1390">Jonathan Boccara</a>.</p>
<p>Thank you!</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1390</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 14 Oct 2018 11:26:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1390</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1389&quot;&gt;Guillermo Blanco Amaro&lt;/a&gt;.

This looks like the &lt;a href=&quot;https://www.fluentcpp.com/2018/01/30/most-vexing-parse/&quot;&gt;most vexing parse&lt;/a&gt;! :)
On the line with:
&lt;pre&gt;&lt;code&gt;
Derived k();
&lt;/code&gt;&lt;/pre&gt;
This is parsed as a function declaration. Try to remove the () or replace them with {} or with &lt;a href=&quot;https://www.fluentcpp.com/2018/09/28/auto-stick-changing-style/&quot;&gt;auto to stick&lt;/a&gt; maybe, and please let me know if there are any other compile errors left. Or alternatively send over a &lt;a href=&quot;http://coliru.stacked-crooked.com&quot; rel=&quot;nofollow&quot;&gt;coliru&lt;/a&gt; link that I could look at.]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1389">Guillermo Blanco Amaro</a>.</p>
<p>This looks like the <a href="https://www.fluentcpp.com/2018/01/30/most-vexing-parse/">most vexing parse</a>! 🙂<br />
On the line with:</p>
<pre><code>
Derived k();
</code></pre>
<p>This is parsed as a function declaration. Try to remove the () or replace them with {} or with <a href="https://www.fluentcpp.com/2018/09/28/auto-stick-changing-style/">auto to stick</a> maybe, and please let me know if there are any other compile errors left. Or alternatively send over a <a href="http://coliru.stacked-crooked.com" rel="nofollow">coliru</a> link that I could look at.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Guillermo Blanco Amaro		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1389</link>

		<dc:creator><![CDATA[Guillermo Blanco Amaro]]></dc:creator>
		<pubDate>Sun, 14 Oct 2018 04:39:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1389</guid>

					<description><![CDATA[Hello Jonathan,

Great article! Thanks a lot for posting it. It took me a while to understand why you would need it but it finally clicked.

I was trying to incorporate this pattern to my pet project but I can&#039;t make it work. I can&#039;t find what&#039;s wrong with it.

This is the error I get:
&#124;&#124;=== Build: Debug in test (compiler: GNU GCC Compiler) ===&#124;
In function ‘int main()’:
error: no matching function for call to ‘dosomething(Derived (&#038;)())’
note: candidate: template void dosomething(const Base&#038;)
note:   template argument deduction/substitution failed:
note:   mismatched types ‘const Base’ and ‘Derived()’

This is the code:
```
template 
struct Base {
    int x;
    Base():x(4){}
};

struct Derived: Base {
    Derived(){}
};

template
void dosomething(Base const&#038; b) {
    std::cout &#060;&#060; b.x &#060;&#060; std::endl;
}

int main() {
    Derived k();
    dosomething(k);
}
```

Why do you think I am getting this error?]]></description>
			<content:encoded><![CDATA[<p>Hello Jonathan,</p>
<p>Great article! Thanks a lot for posting it. It took me a while to understand why you would need it but it finally clicked.</p>
<p>I was trying to incorporate this pattern to my pet project but I can&#8217;t make it work. I can&#8217;t find what&#8217;s wrong with it.</p>
<p>This is the error I get:<br />
||=== Build: Debug in test (compiler: GNU GCC Compiler) ===|<br />
In function ‘int main()’:<br />
error: no matching function for call to ‘dosomething(Derived (&amp;)())’<br />
note: candidate: template void dosomething(const Base&amp;)<br />
note:   template argument deduction/substitution failed:<br />
note:   mismatched types ‘const Base’ and ‘Derived()’</p>
<p>This is the code:<br />
&#8220;`<br />
template<br />
struct Base {<br />
    int x;<br />
    Base():x(4){}<br />
};</p>
<p>struct Derived: Base {<br />
    Derived(){}<br />
};</p>
<p>template<br />
void dosomething(Base const&amp; b) {<br />
    std::cout &lt;&lt; b.x &lt;&lt; std::endl;<br />
}</p>
<p>int main() {<br />
    Derived k();<br />
    dosomething(k);<br />
}<br />
&#8220;`</p>
<p>Why do you think I am getting this error?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Jonathan Boccara		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1374</link>

		<dc:creator><![CDATA[Jonathan Boccara]]></dc:creator>
		<pubDate>Sun, 07 Oct 2018 16:08:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1374</guid>

					<description><![CDATA[In reply to &lt;a href=&quot;https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1356&quot;&gt;Stauros Marinis&lt;/a&gt;.

Thanks for the nice comment. Would you have a code snippet that compiles and doesn&#039;t work at runtime to illustrate the issue?]]></description>
			<content:encoded><![CDATA[<p>In reply to <a href="https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1356">Stauros Marinis</a>.</p>
<p>Thanks for the nice comment. Would you have a code snippet that compiles and doesn&#8217;t work at runtime to illustrate the issue?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Stauros Marinis		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1356</link>

		<dc:creator><![CDATA[Stauros Marinis]]></dc:creator>
		<pubDate>Tue, 02 Oct 2018 13:59:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1356</guid>

					<description><![CDATA[Hello Jonathan,

Thank you very much for such a clear explanation of the pattern.

I have been playing around with it and I believe I have found a serious issue in terms of type-checking.
The problem is that the interface does not force the inherited classes to provide the functions. (i.e. like it would in the case of dynamic polymorphism via pure virtual functions).
As a result if we forget to include the getValue function in a derived class (i.e. Constant42) then the code will compile fine but will blow up trying to run print(c42)]]></description>
			<content:encoded><![CDATA[<p>Hello Jonathan,</p>
<p>Thank you very much for such a clear explanation of the pattern.</p>
<p>I have been playing around with it and I believe I have found a serious issue in terms of type-checking.<br />
The problem is that the interface does not force the inherited classes to provide the functions. (i.e. like it would in the case of dynamic polymorphism via pure virtual functions).<br />
As a result if we forget to include the getValue function in a derived class (i.e. Constant42) then the code will compile fine but will blow up trying to run print(c42)</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Theo		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1096</link>

		<dc:creator><![CDATA[Theo]]></dc:creator>
		<pubDate>Fri, 06 Jul 2018 08:12:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1096</guid>

					<description><![CDATA[Sorry, was a typo. Forgot the &quot;pulic&quot; in an inheritance specification.

Actually I tried this example because I couldn&#039;t get the first CRTP example of https://www.fluentcpp.com/2017/09/12/how-to-return-a-smart-pointer-and-use-covariance/ to work. And the problem there was that there are errors in the code on that page. I will comment over there.]]></description>
			<content:encoded><![CDATA[<p>Sorry, was a typo. Forgot the &#8220;pulic&#8221; in an inheritance specification.</p>
<p>Actually I tried this example because I couldn&#8217;t get the first CRTP example of <a href="https://www.fluentcpp.com/2017/09/12/how-to-return-a-smart-pointer-and-use-covariance/" rel="ugc">https://www.fluentcpp.com/2017/09/12/how-to-return-a-smart-pointer-and-use-covariance/</a> to work. And the problem there was that there are errors in the code on that page. I will comment over there.</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Theo		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-1093</link>

		<dc:creator><![CDATA[Theo]]></dc:creator>
		<pubDate>Thu, 05 Jul 2018 13:57:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-1093</guid>

					<description><![CDATA[I entered the last example literally in VS2017 and got the following error message:

Error	C2662	&#039;double Amount::get_value(void)&#039;: cannot convert &#039;this&#039; pointer from &#039;const Amount&#039; to &#039;Amount &#038;&#039;

And another one for Variable.

Am I doing something stupid?]]></description>
			<content:encoded><![CDATA[<p>I entered the last example literally in VS2017 and got the following error message:</p>
<p>Error	C2662	&#8216;double Amount::get_value(void)&#8217;: cannot convert &#8216;this&#8217; pointer from &#8216;const Amount&#8217; to &#8216;Amount &amp;&#8217;</p>
<p>And another one for Variable.</p>
<p>Am I doing something stupid?</p>
]]></content:encoded>
		
			</item>
		<item>
		<title>
		By: Vladislav Kaplan		</title>
		<link>https://www.fluentcpp.com/2017/05/16/what-the-crtp-brings-to-code/#comment-436</link>

		<dc:creator><![CDATA[Vladislav Kaplan]]></dc:creator>
		<pubDate>Tue, 25 Jul 2017 07:46:00 +0000</pubDate>
		<guid isPermaLink="false">https://www.fluentcpp.com/?p=742#comment-436</guid>

					<description><![CDATA[Thank you]]></description>
			<content:encoded><![CDATA[<p>Thank you</p>
]]></content:encoded>
		
			</item>
	</channel>
</rss>
