title: Why do I get the error 'vector iterators incompatible'?
body: <p>I am writing a small UI for my program. I have the method <code>onMouseMotion()</code>, which I can call in one of two ways (see code); if I call it through <code>std::function</code>, then <code>!=</code> operator in the for loop stop condition produces the run-time exception <code>vector iterators incompatible</code>. Why?</p>
  
  <pre><code>class Widget : public EventHandler
  {
  protected:
      /* ... */
      std::vector&lt;Widget *&gt; children_;                        
      std::function&lt;bool(Event &amp;)&gt; func_;
  
  private:
      bool onMouseMotion(Event &amp;event);
      /* ... */   
  };
  
  Widget::Widget() 
  {
      /* ... */
      func_ = std::bind(&amp;Widget::onMouseMotion, this, std::placeholders::_1);
      /* ... */
  }
  
  bool Widget::processEvent(Event &amp;event)
  {
      if (event.getType() == ui::EventType::MouseMotionEvent) {
  
          /* Method 1 - onMouseMotion works ok */     
          onMouseMotion(event);
  
          /* Method 2 - onMouseMotion throws */
          //func_(event);
  
          return true;
      }
  }
  
  bool Widget::onMouseMotion(Event &amp;event)
  {
      /* exception occurs on the next line, only when using Method 2 above */
      for (auto child = children_.rbegin(); child != children_.rend(); ++child) {}
  }
  </code></pre>
  
  <p><strong>Updates</strong>: </p>
  
  <ul>
  <li>program is single-threaded.</li>
  <li>the exception is thrown when entering the <code>for</code> loop, zero iterations occur.</li>
  <li>compiling with MSVC.</li>
  <li>same exception with an empty <code>for</code> loop.</li>
  <li>rewritten examples to illustrate the <code>std::function</code> problem.</li>
  </ul>