Is there a weak_ptr equivalent to shared_from_this?
<p>I have a class which I know <em>will always</em> be owned by a <code>std::shared_ptr</code>. However passing <code>shared_ptr</code> or even <code>weak_ptr</code> to functions and methods that don't need ownership or lifetime guarantees creates unnecessary overhead. To get around this I often pass raw pointers to functions. The class itself inherits from <code>std::enable_shared_from_this</code> so if the function needs to take ownership of the pointer it can use a method of the class to get a <code>shared_ptr</code>.</p>

<p>This is all working beautifully. However there are occasions where I don't really want to make a <code>shared_ptr</code> from a raw pointer, what I want instead is a <code>weak_ptr</code>.</p>

<p>From what I understand of the usual implementation of <code>std::shared_ptr</code> it has two atomic variables used as reference counters; one for <code>shared_ptr</code>, one for <code>weak_ptr</code>.</p>

<p>If all I have is a raw pointer to my class and I want a <code>weak_ptr</code>, I must first create a <code>shared_ptr</code> and convert it. Doing so means the reference counters are altered like this:</p>

<ul>
<li>Construct <code>shared_ptr</code>, increment <code>shared_ptr</code> counter</li>
<li>Copy construct <code>weak_ptr</code>, increment <code>weak_ptr</code> counter</li>
<li>Allow <code>shared_ptr</code> to go out of scope, decrement <code>shared_ptr</code> counter</li>
</ul>

<p>This seems to go against the idea that "you don't pay for what you don't use". Is there a way for my class to just provide <code>weak_ptr</code> without first creating a <code>shared_ptr</code>?</p>