Search code examples
c++c++11visual-studio-2015stdbind

false-positive error using std::functional in vs2015


I have implemented some test functionals yesterday and everything compiled and worked fine without errors. Today i came back to my PC and my std::bind's are underlined red but compile without error. Seems like Intellisense and the compiler do not agree on the std::bind type. How can I fix this?

#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();
}

The error message is as follows:

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

I do have the same error type (Intellisense saying there is an error, but it compiles just fine) in more sophisticated code, where I used std::mem_fn().


Solution

  • Had the same problem with VS 2015 C++, I hate it. Microsoft drives me crazy.

    For now I am using a nasty work around by moving the code to a static function. In your case it will be similar to the following:

    class MyClass {
        // other code
    
        int doE () {
            return 0;
        }
    
        static int statDoE(MyClass * myClass) {
            return myClass->doE();
        }
    
    }