Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You don't need to template the <code>friend</code> part, but you need to signify that the <code>friend</code> function is a template:</p> <pre><code>friend boost::shared_ptr&lt;Connection&gt; boost::make_shared&lt;&gt;(/* ... */); // ^^ </code></pre> <p>That works with Comeau and current GCC versions but fails with VC. Better would be the following form:</p> <pre><code>friend boost::shared_ptr&lt;Connection&gt; boost::make_shared&lt;Connection&gt;(/* ... */); </code></pre> <p>That works across multiple compilers now - i tested it on VC8, VC10, GCC 4.2, GCC 4.5 and Comeau 4.3.</p> <p>Alternatively using a qualified name to refer to a particular instance of the function template as Martin does <em>should</em> work and does with Comeau, but GCC chokes on it.</p> <p>A useful alternative that doesn't depend on the implementation details of <code>make_shared()</code> (and thus also works with <a href="https://stackoverflow.com/questions/3541632/using-make-shared-with-a-protected-constructor-abstract-interface/3541733#3541733">VC10s TR1 implementation</a>) is to use the <a href="https://stackoverflow.com/questions/3220009/is-this-key-oriented-access-protection-pattern-a-known-idiom">pass-key-idiom</a> for access-protection of the constructor and to befriend the <code>create()</code> function instead, e.g.:</p> <pre><code>class Connection { // ... public: class Key { friend boost::shared_ptr&lt;Connection&gt; create(const ConnectionManagerPtr&amp;, const std::string&amp;); Key() {} }; Connection(const ConnectionManagerPtr&amp;, const std::string&amp;, const Key&amp;); }; boost::shared_ptr&lt;Connection&gt; create(const ConnectionManagerPtr&amp; p, const std::string&amp; s) { return boost::make_shared&lt;Connection&gt;(p, s, Connection::Key()); } </code></pre>
 

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