Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sorry, but I remake this code for GCC-4.4 and can't check for VC 2010. Errors:</p> <ol> <li><p>Wrong template class declaration in template BarInterface -- replace <code>typename</code> to <code>int</code>: <code>template&lt;template&lt;int&gt; class F, int X&gt; class BarInterface</code></p></li> <li><p>Set public to <code>foo()</code> and <code>bar()</code> method: <code>public: void xxx() { self()-&gt;do_xxx(); }</code></p></li> <li>Set public for base class <code>enable_crtp&lt;FX&gt;</code> in classes <code>BarInterface</code> and <code>FooInterface</code>: <code>public enable_crtp&lt; FX &gt;</code></li> <li>Add scope specification for calling <code>self()</code> method in <code>foo()</code> and <code>bar()</code> methods: <code>void xxx() { enable_crtp&lt;FX&gt;::self()-&gt;do_xxx(); }</code></li> </ol> <p>And finally I get working code:</p> <pre><code>template&lt;typename Derived&gt; struct enable_crtp { private: // typedefs typedef enable_crtp Base; public: // casting "down" the inheritance hierarchy Derived const* self() const { return static_cast&lt;Derived const*&gt;(this); } // write the non-const version in terms of the const version // Effective C++ 3rd ed., Item 3 (p. 24-25) Derived* self() { return const_cast&lt;Derived*&gt;(static_cast&lt;Base const*&gt;(this)-&gt;self()); } protected: // disable deletion of Derived* through Base* // enable deletion of Base* through Derived* ~enable_crtp() { // no-op } }; template&lt;typename FX&gt; class FooInterface : public enable_crtp&lt; FX &gt; { public: // interface void foo() { enable_crtp&lt;FX&gt;::self()-&gt;do_foo(); } }; class FooImpl : public FooInterface&lt; FooImpl &gt; { private: // implementation friend class FooInterface&lt; FooImpl &gt; ; void do_foo() { std::cout &lt;&lt; "Foo\n"; } }; class AnotherFooImpl : public FooInterface&lt; AnotherFooImpl &gt; { private: // implementation friend class FooInterface&lt; AnotherFooImpl &gt;; void do_foo() { std::cout &lt;&lt; "AnotherFoo\n"; } }; template&lt;template&lt;int&gt; class F, int X&gt; class BarInterface : public enable_crtp&lt; F&lt;X&gt; &gt; { public: // interface void bar() { enable_crtp&lt; F&lt;X&gt; &gt;::self()-&gt;do_bar(); } }; template&lt; int X &gt; class BarImpl : public BarInterface&lt; BarImpl, X &gt; { private: // implementation friend class BarInterface&lt; ::BarImpl, X &gt;; void do_bar() const { std::cout &lt;&lt; X &lt;&lt; "\n"; } }; int main() { FooImpl f1; AnotherFooImpl f2; BarImpl&lt; 1 &gt; b1; BarImpl&lt; 2 &gt; b2; f1.foo(); f2.foo(); b1.bar(); b2.bar(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload