0

Came across a strange compilation error while migrating some old code, which made use of the boost library, to C++11. Specifically, using Visual Studio 2013 Professional Update 5.

The following example code fails to compile with error C3848. However, if you change the call to std::bind to boost::bind, then the code compiles as expected.

#include< functional >
#include< ppl.h >
//#include< boost/bind.hpp >

class foo {
public:
    bool function() const {
        return true;
    }
};

int main(int argc, char* argv[]) {
    foo f;
    Concurrency::parallel_for(0, 5, std::bind(&foo::function, &f));
    //Concurrency::parallel_for(0, 5, boost::bind(&foo::function, &f));
    return 0;
}

I was expecting std::bind and boost::bind to be fully interchangeable, but that doesn't appear to be the case.

Can anyone suggest how to get the above example to compile using std::bind?

  • They both compile on VS2015. In some cases, `std::bind` is not a drop in replacement for `boost::bind` because the latter overloads logical and relational operators, while the former doesn't. There's also `boost::protect` which prevents evaluation of a nested `bind`, while the `std` version has no such equivalent. But none of those differences apply to your example. – Praetorian Dec 03 '15 at 21:50
  • Not sure, but try using `std::bind(&foo::function, std::cref(f));` – Grigorii Chudnov Dec 03 '15 at 22:09
  • @Grigoriy Chudnov Tried to use `std::bind(&foo::function, std::cref(f));` as suggested, but the compiler still returns error C3848. – A. Myers Dec 03 '15 at 22:27
  • Is there a reason not to just use a lambda here? – Rick Dec 16 '15 at 17:48

0 Answers0