Boost日志非常量位域编译错误(向后兼容性问题)
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我得到一个编译错误:
不能绑定位域'bf。BF::b' to 'unsigned int&'
使用boost 1.57编译并运行代码(打印:[2016-09-19 20:21:33.018112][0x000007fd1d5be672] [info]一个信息严重性消息0)
注意:
- cout当然可以处理这个(所以我认为这不仅仅是一个向后兼容性问题,而是一个bug)
- 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;
}
整数被复制(按值获取),所以没有绑定问题
相关文章:
- 提升序列化 1:73 的向后兼容性问题
- C++ ABI 兼容性问题/张量流
- Oracle OCCI 和 g++ 7.1 的兼容性问题
- python 和 swig 版本兼容性问题
- Tensorflow C++ API 和 Caffe 兼容性问题与 Glog 相关
- 潜在 DLL 兼容性问题的摘要?
- D3DX10.H和D3DCOMPILER.H兼容性问题
- 类型定义之间的兼容性问题
- 在使用__file__,__line__时,是否存在任何兼容性问题
- Mac和Linux机器之间的正则兼容性问题
- Clang和Visual C 结构兼容性问题
- 从C结构继承的可能兼容性问题
- UNICODESTRING兼容性问题
- 错误:在armadillo和oracle occi兼容性问题中,对"剑"的引用不明确
- box2d光线投射向后兼容性问题
- .NET 和 Linux 之间的 GUID/UUID 兼容性问题
- Boost::asio-winsock和winsock 2的兼容性问题
- 使用控制台的可视C++:while 循环的字符/字符串兼容性问题
- 7-Zip源代码兼容性问题
- Java本机接口(JNI)是否受到c++ ABI兼容性问题的影响