Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>So, I'm going to cheat by changing your syntax.</p> <pre><code>template&lt;typename T&gt; using Ptr = T::template ptr&lt;T&gt;; template&lt;typename T&gt; using WPtr = T::template wptr&lt;T&gt;; struct Pointerable { template&lt;typename T&gt; using ptr = std::shared_ptr&lt;T&gt;; template&lt;typename T&gt; using wptr = std::weak_ptr&lt;T&gt;; }; class Foo : public Pointerable { }; Ptr&lt;Foo&gt; x = std::make_shared&lt;Foo&gt;( ... ); WPtr&lt;Foo&gt; y = x; </code></pre> <p>here, the only purpose of <code>Pointerable</code> is to tag your types that you want to be used. <code>Ptr&lt;Foo&gt;</code> and <code>WPtr&lt;Foo&gt;</code> could be written to work on any class:</p> <pre><code>template&lt;typename T&gt; using Ptr = std::shared_ptr&lt;T&gt;; template&lt;typename T&gt; using WPtr = std::weak_ptr&lt;T&gt;; </code></pre> <p>but I suppose you want to be able to tag objects as "managed by smart pointers" for whatever reason.</p> <p>We could even make <code>Pointerable</code> completely empty, if you don't like that <code>template</code> boilerplate in it:</p> <pre><code>struct Pointerable {}; </code></pre> <p>and add a traits class:</p> <pre><code>template&lt;typename T&gt; struct is_pointerable : std::is_base_of&lt;Pointerable, T&gt; {}; </code></pre> <p>which we then use:</p> <pre><code>template&lt;typename T, typename=typename std::enable_if&lt; is_pointerable&lt;T&gt;::value &gt;::type &gt; using Ptr = std::shared_ptr&lt;T&gt;; </code></pre> <p>but I think the boilerplate works better -- SFINAE <code>using</code> seems overkill.</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