我正在尝试在Ubuntu 18.04 LTS上使用gcc 7.3.0使用选项-std = c ++ 14编译以下代码:
auto createLogFormatter()
{
boost::log::expressions::stream_type stream;
stream << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S ")
<< boost::log::expressions::attr<int>("Severity")
<< " " // ERROR HERE
<< boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type>("ThreadID")
<< " "
<< boost::log::expressions::smessage;
return stream;
}
越来越奇怪的错误:
Log.cpp:51:13: error: no match for ‘operator<<’ (operand types are ‘boost::log::v2_mt_posix::expressions::aux::make_output_actor<boost::phoenix::actor<boost::log::v2_mt_posix::expressions::aux::attribute_output_terminal<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::phoenix::argument<2> >, 0> >, boost::posix_time::ptime, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::aux::light_function<void(boost::log::v2_mt_posix::basic_formatting_ostream<char>&, const boost::posix_time::ptime&)> > >, boost::log::v2_mt_posix::expressions::attribute_actor<int, boost::log::v2_mt_posix::fallback_to_none, void, boost::phoenix::actor>, int, false>::type {aka boost::phoenix::actor<boost::log::v2_mt_posix::expressions::aux::attribute_output_terminal<boost::phoenix::actor<boost::log::v2_mt_posix::expressions::aux::attribute_output_terminal<boost::phoenix::actor<boost::proto::exprns_::basic_expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::phoenix::argument<2> >, 0> >, boost::posix_time::ptime, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::aux::light_function<void(boost::log::v2_mt_posix::basic_formatting_ostream<char>&, const boost::posix_time::ptime&)> > >, int, boost::log::v2_mt_posix::fallback_to_none, boost::log::v2_mt_posix::to_log_fun<void> > >}’ and ‘const char [2]’)
stream << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S ")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<< boost::log::expressions::attr<int>("Severity")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<< " "
^~~~~~
根据Boost示例here,这种方法应该有效:
expr::stream
<< expr::attr< unsigned int >("LineID")
<< ": <" << logging::trivial::severity
<< "> " << expr::smessage
但是我遇到了这样的错误。 我在做什么错了?
答案 0 :(得分:1)
编译器错误很可能是由于缺少包含引起的。 Lambda表达式运算符由boost/phoenix/operator.hpp
中的Boost.Phoenix提供。或者,您可以只包含boost/log/expressions.hpp
,它提供所有Boost.Log表达式节点以及运算符。
但是除此之外,您的createLogFormatter
函数是不正确的,因为它会丢弃整个格式化程序表达式。您不应该创建和返回stream
对象,它只是启动格式化程序表达式的终端。您需要返回由于lambda表达式而创建的函数对象。
auto createLogFormatter()
{
auto fmt = stream << boost::log::expressions::format_date_time<boost::posix_time::ptime>("TimeStamp", "%Y-%m-%d %H:%M:%S ")
<< boost::log::expressions::attr<int>("Severity")
<< " " // ERROR HERE
<< boost::log::expressions::attr<boost::log::attributes::current_thread_id::value_type>("ThreadID")
<< " "
<< boost::log::expressions::smessage;
return fmt;
}
如果要使用稳定的返回类型,可以使用boost::log::formatter
来包装表达式。