Note that there are some explanatory texts on larger screens.

plurals
  1. POtypedef with CRTP doesn't work when inheritance is used
    text
    copied!<p>Is there any ways to define a type with a same name for classes in inheritance relationship by using CTRP? I tried the following code but got <code>error: member 'ptr_t' found in multiple base classes of different types</code> from <code>clang++</code>.</p> <pre><code>#include &lt;iostream&gt; #include &lt;tr1/memory&gt; template &lt;typename T&gt; class Pointable { public: // define a type `ptr_t` in the class `T` publicly typedef std::tr1::shared_ptr&lt;T&gt; ptr_t; }; class Parent : public Pointable&lt;Parent&gt; { public: Parent() { std::cout &lt;&lt; "Parent created" &lt;&lt; std::endl; } ~Parent() { std::cout &lt;&lt; "Parent deleted" &lt;&lt; std::endl; } }; class Child : public Parent, public Pointable&lt;Child&gt; { public: Child() { std::cout &lt;&lt; "Child created" &lt;&lt; std::endl; } ~Child() { std::cout &lt;&lt; "Child deleted" &lt;&lt; std::endl; } }; int main(int argc, char** argv) { Child::ptr_t child_ptr(new Child()); Parent::ptr_t parent_ptr(new Parent()); return 0; } </code></pre> <p>Of course, the following one is OK (but it's redundant and go against the DRY principle).</p> <pre><code>class Parent { public: typedef std::tr1::shared_ptr&lt;Parent&gt; ptr_t; Parent() { std::cout &lt;&lt; "Parent created" &lt;&lt; std::endl; } ~Parent() { std::cout &lt;&lt; "Parent deleted" &lt;&lt; std::endl; } }; class Child : public Parent { public: typedef std::tr1::shared_ptr&lt;Child&gt; ptr_t; Child() { std::cout &lt;&lt; "Child created" &lt;&lt; std::endl; } ~Child() { std::cout &lt;&lt; "Child deleted" &lt;&lt; std::endl; } }; </code></pre> <p>If there is no ways to achieve this behavior by using CRTP, why that is prohibited?</p>
 

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