boost log non-const bitfield 编译错误(向后兼容问题)
boost log non-const bitfield compilation error (backward compatibility issue)
我从 http://www.boost.org/doc/libs/1_61_0/libs/log/example/doc/tutorial_trivial_flt.cpp 中获取示例并添加了一个位域打印:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
namespace logging = boost::log;
//[ example_tutorial_trivial_with_filtering
void init()
{
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
struct BF {
unsigned int b : 8;
BF() : b(0) {}
};
int main(int, char*[])
{
init();
BF bf;
BOOST_LOG_TRIVIAL(info) << "An informational severity message " << bf.b;
return 0;
}
//]
使用 boost 1.61 时出现编译错误:
cannot bind bitfield 'bf.BF::b' to 'unsigned int&'
使用 boost 1.57 编译代码并 运行(打印:[2016-09-19 20:21:33.018112] [0x000007fd1d5be672] [info] 信息严重性消息 0)
注意:
- cout 当然可以处理这个(所以我认为这不仅仅是一个向后兼容性问题,而是一个错误)
- boost 1.61 可以处理 const 位域,例如
BOOST_LOG_TRIVIAL(info) << "An informational severity message " << BF().b;
我正在寻找解决方法。
建议?
最简单的解决方法是将位域转换为完整整数。你可以用演员表来做到这一点:
BOOST_LOG_TRIVIAL(info) << "An informational severity message "
<< static_cast< unsigned int >(BF().b);
我找到了一个解决方法 - 为所有无符号整数的 record_ostream 重载运算符<<:
#include <sys/types.h>
namespace logging = boost::log;
typedef logging::basic_formatting_ostream< logging::record_ostream::char_type > formatting_ostream_type;
logging::record_ostream& operator << (logging::record_ostream& strm, u_int8_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
logging::record_ostream& operator << (logging::record_ostream& strm, u_int16_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
logging::record_ostream& operator << (logging::record_ostream& strm, u_int32_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
logging::record_ostream& operator << (logging::record_ostream& strm, u_int64_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
整数已被复制(按值获取)因此没有绑定问题
我从 http://www.boost.org/doc/libs/1_61_0/libs/log/example/doc/tutorial_trivial_flt.cpp 中获取示例并添加了一个位域打印:
#include <boost/log/core.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/expressions.hpp>
namespace logging = boost::log;
//[ example_tutorial_trivial_with_filtering
void init()
{
logging::core::get()->set_filter
(
logging::trivial::severity >= logging::trivial::info
);
}
struct BF {
unsigned int b : 8;
BF() : b(0) {}
};
int main(int, char*[])
{
init();
BF bf;
BOOST_LOG_TRIVIAL(info) << "An informational severity message " << bf.b;
return 0;
}
//]
使用 boost 1.61 时出现编译错误:
cannot bind bitfield 'bf.BF::b' to 'unsigned int&'
使用 boost 1.57 编译代码并 运行(打印:[2016-09-19 20:21:33.018112] [0x000007fd1d5be672] [info] 信息严重性消息 0)
注意:
- cout 当然可以处理这个(所以我认为这不仅仅是一个向后兼容性问题,而是一个错误)
- boost 1.61 可以处理 const 位域,例如
BOOST_LOG_TRIVIAL(info) << "An informational severity message " << BF().b;
我正在寻找解决方法。 建议?
最简单的解决方法是将位域转换为完整整数。你可以用演员表来做到这一点:
BOOST_LOG_TRIVIAL(info) << "An informational severity message "
<< static_cast< unsigned int >(BF().b);
我找到了一个解决方法 - 为所有无符号整数的 record_ostream 重载运算符<<:
#include <sys/types.h>
namespace logging = boost::log;
typedef logging::basic_formatting_ostream< logging::record_ostream::char_type > formatting_ostream_type;
logging::record_ostream& operator << (logging::record_ostream& strm, u_int8_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
logging::record_ostream& operator << (logging::record_ostream& strm, u_int16_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
logging::record_ostream& operator << (logging::record_ostream& strm, u_int32_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
logging::record_ostream& operator << (logging::record_ostream& strm, u_int64_t value) {
static_cast< formatting_ostream_type& >(strm) << value;
return strm;
}
整数已被复制(按值获取)因此没有绑定问题