title: Using shared_from_this in templated classes
body: <p>I have a resource manager that, like Andrei Alexandrescu proposed in the book Modern C++ Design, follows a policy based design. I am having trouble though, because my resource manager needs to be able to provide references to itself to the managed resources by <code>shared_from_this()</code>.</p>
  
  <p>I built a minimal example reproducing my problem, which results you can see <a href="http://ideone.com/VbJo3f" rel="noreferrer">here</a>.</p>
  
  <p>Basically I have some managed resource that needs a reference to its manager:</p>
  
  <pre><code>template &lt;typename T&gt;
  class managed_resource
  {
          typedef std::shared_ptr&lt;manager&lt;T&gt;&gt; manager_ptr;
      public:
          managed_resource(manager_ptr const &amp; parent)
              : parent_(parent)
          {
          }
  
          /* ... */
  
      private:
          manager_ptr parent_;
  };
  </code></pre>
  
  <p>And a manager that stores and provides resources:</p>
  
  <pre><code>template &lt;typename Policy&gt;
  class manager
      : Policy
      , std::enable_shared_from_this&lt;manager&lt;Policy&gt;&gt;
  {
          typedef managed_resource&lt;Policy&gt; resource;
          typedef std::shared_ptr&lt;resource&gt; resource_ptr;
      public:
          resource_ptr get_resource(std::string const &amp; name)
          {
              Policy &amp; p = *this;
              if(p.find(name))
              {
                  return p.get(name);
              }
              resource_ptr res = std::make_shared&lt;resource&gt;(shared_from_this());
              p.store(name, res);
              return res;
          }
  };
  </code></pre>
  
  <p>As you can see, the storing itself is policy-based. While the manager does create the resources, the policy can freely decide between various approaches of storing the information (it could e.g. choose to not store anything and create new resources every time).</p>
  
  <p>This is an example of a storage policy:</p>
  
  <pre><code>class map_policy
  {
          typedef std::shared_ptr&lt;managed_resource&lt;map_policy&gt;&gt; resource_ptr;
          typedef std::map&lt;std::string, resource_ptr&gt; resources;
  
      public:
          bool find(std::string const &amp; name)
          {
              resources::iterator res_it = resources_.find(name);
              return res_it != resources_.end();
          }
  
          resource_ptr get(std::string const &amp; name)
          {
              resources::iterator res_it = resources_.find(name);
              return res_it-&gt;second;
          }
  
          void store(std::string const &amp; name, resource_ptr const &amp; res)
          {
              resources_[name] = res;
          }
  
      private:
          resources resources_;
  };
  </code></pre>
  
  <p>But I get a compilation error:</p>
  
  <pre><code>error: there are no arguments to ‘shared_from_this’ that depend 
         on a template parameter, so a declaration of 
         ‘shared_from_this’ must be available
  error: ‘std::enable_shared_from_this&lt;manager&lt;map_policy&gt; &gt;’ is 
         an inaccessible base of ‘manager&lt;map_policy&gt;’
  </code></pre>
  
  <p>For full compilation output see the <a href="http://ideone.com/VbJo3f" rel="noreferrer">minimal example</a>.</p>
  
  <p>Is it impossible to use <code>std::enable_shared_from_this</code> and <code>shared_from_this()</code> within policy based design? If not, what is the proper way of using it?</p>