使用muduo网络库编译出现error: reference to ‘_1’ is ambiguous


某程序使用了muduo网络库,而编译时报错:

其实看编译报错原因就很明显了
std::placeholders::_1 和 boost库的extern const _Placeholder<1> _1 冲突了。

而用户代码并没有using namesapce std::placeholders
而是想使用boost::bind
如:

#include 
#include 
#include 

#include 

#include 

using namespace muduo;
using namespace muduo::net;

class TestServer
{
 public:
  TestServer(EventLoop* loop,
             const InetAddress& listenAddr)
    : loop_(loop),
      server_(loop, listenAddr, "TestServer")
  {
    server_.setConnectionCallback(
        boost::bind(&TestServer::onConnection, this, _1));
    server_.setMessageCallback(
        boost::bind(&TestServer::onMessage, this, _1, _2, _3));
  server_.setWriteCompleteCallback(
      boost::bind(&TestServer::onWriteComplete, this, _1));

原因是muduo库中使用了std::placeholders命名空间
在muduo/net文件夹下搜索
$ grep using ./*
可找到在Callbackes.h中

如果在用户代码中真的要用boost::bind
可将其注释掉

或者使用std::bind代替boost::bind

参考:
Why boost::bind insists pulling boost::placeholders into global namespace?