我使用的是websocketcpp和boost 1.71库。该代码用于boost 1.58,但在升级两个库后,它将无法编译。c++编译器为g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0,代码如下:
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));
}
我用#define BOOST_BIND_NO_PLACEHOLDERS
排序。但是,这会给出以下错误:
/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
就像大家说的那样,不要using namespace
。
另外,使用最近的boost,你应该避免使用BOOST_BIND_GLOBAL_PLACEHOLDERS
,而不是<boost/bind.hpp>
,包括<boost/bind/bind.hpp>
(默认情况下不再这样做)。
这是您看到的冲突的来源,因为property_tree
使用boost bind(如错误消息所示)。
完全不相关:不要(ab)使用PropertyTree,如果它是一个JSON库。可以使用Boost JSON或其他JSON库。
同样,无关的是,您不需要所有的
using
s。您已经可以使用ADL来减少类型:#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); }
参见Live On Coliru