简体   繁体   中英

C++ and boost 1.71 - error: reference to ‘_1’ is ambiguous

I am using the libraries websocketcpp and boost 1.71. The code used to work with boost 1.58 but after upgrading both libraries, it won't compile. The C++ compiler is g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0 and the code is the following:

using boost::property_tree::ptree;
using boost::property_tree::read_json;
using boost::property_tree::write_json;

using websocketpp::connection_hdl;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
//.......
{
    srv.init_asio();
    srv.set_reuse_addr(true);
    srv.set_open_handler(bind(&WsServer::on_open, this, websocketpp::lib::placeholders::_1));
    srv.set_close_handler(bind(&WsServer::on_close, this, websocketpp::lib::placeholders::_1));
    srv.set_message_handler(bind(&WsServer::on_message, this, websocketpp::lib::placeholders::_1, websocketpp::lib::placeholders::_2));
}

I sorted out using #define BOOST_BIND_NO_PLACEHOLDERS . However, that will give the following error:

/usr/include/boost/bind/bind.hpp:319:35: error: no match for call to ‘(boost::_mfi::mf1<void, boost::property_tree::json_parser::detail::standard_callbacks<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >, char>) (
boost::property_tree::json_parser::detail::standard_callbacks<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >&, std::_Placeholder<1>&)’
  319 |         unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//...
/usr/include/boost/bind/mem_fn_template.hpp:176:25: note:   template argument deduction/substitution failed:

usr/include/boost/bind/mem_fn_template.hpp:184:7: note: candidate: ‘R boost::_mfi::mf1<R, T, A1>::operator()(T&, A1) const [with R = void; T = boost::property_tree::json_parser::detail::standard_callbacks<boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> > >; A1 = char]’
  184 |     R operator()(T & t, A1 a1) const
      |       ^~~~~~~~
/usr/include/boost/bind/mem_fn_template.hpp:184:28: note:   no known conversion for argument 2 from ‘std::_Placeholder<1>’ to ‘char’
  184 |     R operator()(T & t, A1 a1) const

Like everybody says, don't using namespace .

Also, using more recent boost you should avoid BOOST_BIND_GLOBAL_PLACEHOLDERS , and instead of <boost/bind.hpp> include <boost/bind/bind.hpp> (which no longer does that by default.

This is the source of the conflict you see, since property_tree uses boost bind (as the error messages show).

Completely unrelated: DO NOT (ab)use PropertyTree as if it is a JSON library. Instead use Boost JSON or some other JSON library.


Also, unrelated, you don't need all the using s anyway. You can already use ADL to reduce the typing:

 #include <boost/property_tree/json_parser.hpp> #include <iostream> int main() { boost::property_tree::ptree pt; std::istringstream str(R"({"hello":"world"})"); read_json(str, pt); write_json(std::cout, pt); }

See Live On Coliru

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM