为什么我得到错误'vector iterators incompatible'?

Dan*_*tor 7 c++ iterator vector visual-c++ std-function

我正在为我的程序编写一个小UI.我有方法onMouseMotion(),我可以用两种方式之一调用(参见代码); 如果我通过它std::function,那么!=for循环停止条件中的运算符会产生运行时异常vector iterators incompatible.为什么?

class Widget : public EventHandler
{
protected:
    /* ... */
    std::vector<Widget *> children_;                        
    std::function<bool(Event &)> func_;

private:
    bool onMouseMotion(Event &event);
    /* ... */   
};

Widget::Widget() 
{
    /* ... */
    func_ = std::bind(&Widget::onMouseMotion, this, std::placeholders::_1);
    /* ... */
}

bool Widget::processEvent(Event &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 &event)
{
    /* exception occurs on the next line, only when using Method 2 above */
    for (auto child = children_.rbegin(); child != children_.rend(); ++child) {}
}
Run Code Online (Sandbox Code Playgroud)

更新:

  • 程序是单线程的.
  • 进入for循环时抛出异常,发生零迭代.
  • 用MSVC编译.
  • for循环的相同异常.
  • 重写的例子来说明std::function问题.

dav*_*pfx 3

所以很明显

  • auto为 child 定义一个迭代器类型,由编译器静态确定(它不能在调用之间更改)。
  • 的类型与andchild兼容rbegin()rend()
  • 的类型在直接调用时child兼容关系运算符,但在通过包装器rbegin()调用时不兼容bind()
  • 由于 的类型不能改变,因此在第二种情况下,child的类型必须改变。rend()

我看到以下可能性。

  1. 向量是一个成员,因此迭代器类型将是一个指向成员的指针,而指向成员的指针对其使用方式有一些限制。
  2. 第一种情况下的值this可能与绑定中捕获的值不同(例如,基类与派生类)
  3. 的值this可以通过包装器传递,这会改变其类型行为。

总的来说,这很可能是 MSVC 错误。提供的代码无法编译,我不愿意尝试修改,并且可能无法重现该错误。如果您可以发布可编译的重现案例,我很乐意进一步调查并更新答案。