Reputation: 171
I was a user of VS2013 and I often use initialization as follows and it was working like a charm:
MyClass::MyClass myRoutine(){
std::function<double(double)> oFunc = std::bind(&Myclass::myfunction, this, std::placeholders::_1);
}
MyClass::MyClass myfunction(double & inX){
return 0;
}
I decided to upgrade to VS2015, but the compiler reports errors:
error C2440: 'initializing': cannot convert from 'std::_Binder<std::_Unforced,double (__thiscall MyClass::* )(double &),MyClass *const ,const std::_Ph<1> &>' to 'std::function<double (double)>'
What happened?
Upvotes: 0
Views: 1138
Reputation: 4052
Your function takes its argument by reference (double &
) while your are trying to assign it to the function with signature that takes by value (double
).
This has compiled in VS2013 most likely due to conformance bugs (maybe some weird interaction with non-const ref to temporary MSVC extension). Basically it's not a valid C++ code. VS2015 is leagues ahead of previous VSes (VS2017 is even more so) in terms of standard support and conformance so this faulty code is no longer compiling.
Anyway, std::bind is almost entirely obsoleted by lambdas, which should be supported by VS2010+. They produce better code, better warnings/errors, easier to write and debug.
For example, your code could'be rewritten like this:
std::function<double(double)> oFunc = [this](double x){ return myfunction(x); };
Or like this if you actually want to pass by reference:
std::function<double(double &)> oFunc = [this](double &x){ return myfunction(x); };
Notice how shorter and simpler it is.
Upvotes: 1
Reputation: 136296
Function signatures do not match: double(double)
vs double(double&)
. Hence the compilation error. One of these needs to be changed, e.g.:
double MyClass::myfunction(double inX){
return 0;
}
Upvotes: 5