C++11 placeholders with boost
boost, c++, c++11
Solution
Let's see how the includes work:
`#include <boost/signals2.hpp>` includes `#include <boost/signals2/signal.hpp>` which includes `#include <boost/signals2/slot.hpp>` which includes `#include <boost/bind.hpp>` which includes `#include <boost/bind/bind.hpp>` which includes `include <boost/bind/placeholders.hpp>`, which uses `static boost::arg<1> _1;`* in the global namespace, hence the ambiguity.
*: Technically, `_1` is in an unnamed namespace, but it visible due to a using directive.
One workaround is define the following at the top of your file so that `<boost/bind/placeholders.hpp>` is not included:
#define BOOST_BIND_NO_PLACEHOLDERS
Problem
This code ... ``` int main() { using namespace std::placeholders; ClassA a; ClassB b, b2; a.SigA.connect( std::bind(&ClassB::PrintFoo, &b) ); a.SigB.connect( std::bind(&ClassB::PrintInt, b, _1)); a.SigB.connect( std::bind(&ClassB::PrintInt, &b2, _1)); a.SigA(); a.SigB(4); } ``` Gives the compilation error, "error: reference to '_1' is ambiguous" It can be fixed by fully qualifying the placeholders ... ``` int main() { // using namespace std::placeholders; ClassA a; ClassB b, b2; a.SigA.connect( std::bind(&ClassB::PrintFoo, &b) ); a.SigB.connect( std::bind(&ClassB::PrintInt, b, std::placeholders::_1)); a.SigB.connect( std::bind(&ClassB::PrintInt, &b2, std::placeholders::_1)); a.SigA(); a.SigB(4); } ``` ...but why doesn't the first code snippet work? EDIT Just to prevent any ambiguity, I am compiling with Clang and Boost 1.52 with `--stdlib=libc++ -std=c++0x` and the entire code block is this ... ``` #include <boost/signals2.hpp> #include <iostream> struct ClassA { boost::signals2::signal<void ()> SigA; boost::signals2::signal<void (int)> SigB; }; struct ClassB { void PrintFoo() { std::cout << "Foo" << std::endl; } void PrintInt(int i) { std::cout << "Bar: " << i << std::endl; } }; int main() { // using namespace std::placeholders; ClassA a; ClassB b, b2; a.SigA.connect( std::bind(&ClassB::PrintFoo, &b) ); a.SigB.connect( std::bind(&ClassB::PrintInt, b, std::placeholders::_1)); a.SigB.connect( std::bind(&ClassB::PrintInt, &b2, std::placeholders::_1)); a.SigA(); a.SigB(4); } ```