title: Why doesn't std::weak_ptr have operator->?
body: <p>It could be implemented thusly:</p>
  
  <pre><code>  std::shared_ptr&lt;T&gt; operator-&gt;() {
        auto shared = lock();
        if(shared == nullptr) {
            throw std::bad_weak_ptr(); // or some other exception
        }
        return shared;
    }
  </code></pre>
  
  <p><a href="http://coliru.stacked-crooked.com/a/9e903500841c9313" rel="nofollow">Live Demo</a></p>
  
  <p><em>Why did the <strong>authors</strong> of weak_ptr decide to not have operator->?</em> (They must have thought of it)</p>
  
  <p>I can think of potential reasons but I wonder what the <strong>official reason</strong> is, if one exists. Potential reasons:</p>
  
  <ul>
  <li>Discourage extra increment/decrement of reference count for multiple calls</li>
  <li>Encourage explicit locking rather than (somewhat hidden) exceptions</li>
  </ul>
  
  <p>If you are confused about the lifetime of the returned shared_ptr, see <a href="http://www.stroustrup.com/wrapper.pdf" rel="nofollow">this paper.</a></p>
  
  <p>Also, someone asked why would one use a weak_ptr if you expect it to not be expired? Answer: cycles.</p>