Std :: bind when std :: function collides with Clang

I am having trouble understanding some of the subtleties when combining std :: bind with std :: function.

I minimized my problems in the following code snippet:

#include <functional> #include <iostream> void bar(int x) { std::cout << "Hello." << std::endl; } int main(int argc, char* argv[]) { std::function<void(int)> f1 = std::bind(bar, std::placeholders::_1); // CRASHES with clang, works fine in VS2010 and VS2012 std::function<void()> f2 = std::bind(f1, 1); f2(); return 0; } 

Note the explicit conversion to std :: function <> (replacing std::function<void()> with auto works fine when building f2 ).

Creating f2 by copying the f1 object happens with Clang OS X (Xcode 5.0.1, OS X 10.9 SDK), but works fine with VS2010 / VS2012. After a shockingly long stop code, the code fails with EXC_BAD_ACCESS - note that this is when building a function object, and not when calling it.

Is this a bug in the library implementation or a problem with my code?

The workaround is to explicitly call the operator () element, for example:

 std::function<void()> f2 = std::bind(&std::function<void(int)>::operator(), f1, 1); 

but this is due to a caution since it does not compile on VS2012 (but works in VS2010). This seems to be a bug with VS2012 .

+7
c ++ c ++ 11 xcode clang
source share
1 answer

Your code works fine with clang 3.4 (trunk, 194324). It also works with gcc 4.7.2.

I don't get any warnings with -Wall -Wextra -pedantic (except for those that complain about unused x, argc and argv options).

It seems that the clang implementation was a bug in the past, but has been fixed.

+2
source share

All Articles