在 vs2015 中使用 std::functional 的误报错误

false-positive error using std::functional in vs2015

我昨天已经实现了一些测试功能,所有的东西都编译好了,没有错误。今天我回到我的电脑上,我的 std::bind 有红色下划线,但编译没有错误。似乎 Intellisense 和编译器不同意 std::bind 类型。我该如何解决这个问题?

#include <functional>

class MyClass {
public:
    int doE() {
        return 0;
    }

    int doF() {
        return 1;
    }
};


void main()
{
    MyClass obj;
    std::function<int()> f = std::bind(&MyClass::doE, obj); // underlined red
    std::cout << f();
}

报错信息如下:

Error (active)
    no suitable user-defined conversion from "std::_Binder<std::_Unforced, int (MyClass::*)(), MyClass &>" to "std::function<int ()>" exists
    functionals
    c:\functionals\functionals\thirdFunctionOnObject.h

我在更复杂的代码中确实有相同的错误类型(Intellisense 说有错误,但它编译得很好),我在其中使用 std::mem_fn().

VS 2015 C++ 也有同样的问题,我讨厌它。微软让我抓狂。

现在我正在通过将代码移动到静态函数来使用令人讨厌的变通方法。在您的情况下,它将类似于以下内容:

class MyClass {
    // other code

    int doE () {
        return 0;
    }

    static int statDoE(MyClass * myClass) {
        return myClass->doE();
    }

}