title: Using weak_ptr to implement the Observer pattern
body: <p>What I have so far is:</p>
  
  <p>Observer.h</p>
  
  <pre><code>class Observer
  {
  public:
      ~Observer();
      virtual void Notify() = 0;
  protected:
      Observer();
  };
  
  class Observable
  {
  public:
      ~Observable();
      void Subscribe( std::shared_ptr&lt;Observer&gt; observer );
      void Unsubscribe( std::shared_ptr&lt;Observer&gt; observer );
      void Notify();
  protected:
      Observable();
  private:
      std::vector&lt;std::weak_ptr&lt;Observer&gt;&gt; observers;
  };
  </code></pre>
  
  <p>Observer.cpp</p>
  
  <pre><code>void Observable::Subscribe( std::shared_ptr&lt;Observer&gt; observer )
  {
      observers.push_back( observer );
  }
  
  void Observable::Unsubscribe( std::shared_ptr&lt;Observer&gt; observer )
  {
      ???
  }
  
  void Observable::Notify()
  {
      for ( auto wptr : observers )
      {
          if ( !wptr.expired() )
          {
              auto observer = wptr.lock();
              observer-&gt;Notify();
          }
      }
  }
  </code></pre>
  
  <p>(de/constructors are implemented here but empty, so I've left them out)</p>
  
  <p>What I'm stuck on is how to implement the Unsubscribe procedure. I came across the erase - remove - end idiom, but I understand that it will not work "out of the box" with how I have setup my Observable. How do I inspect the weak_ptr elements in the observers vector such that I can remove the desired Observer?</p>
  
  <p>I'm also looking for some advice on what the parameter type should be for my Un/Subscribe procedures. Would it be better to use <code>std::shared_ptr&lt;Observer&gt;&amp;</code> or <code>const std::shared_ptr&lt;Observer&gt;&amp;</code>, since we will not be modifying it?</p>
  
  <p>I really do not want to have Observables owning their Observers, as it seems to betray the intentions of the pattern, and is certainly not how I want to structure the rest of the project that will ultimately be making use of the pattern. That said, an added layer of security / automation that I am considering is to have Observers store a mirror vector of weak_ptr. An Observer on its way out could then unsubscribe from all Observables it had subscribed to, and an Observable on its way out could erase the back-reference to itself from each of the Observers observing it. Evidently the two classes would be friends in such a scenario.</p>