0

我把例如從http://www.boost.org/doc/libs/1_61_0/libs/log/example/doc/tutorial_trivial_flt.cpp並增加了一個位域打印:升壓日誌非const位域編譯錯誤(向後兼容問題)

#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; 
} 
//] 

有了提升1.61我得到一個編譯錯誤:

cannot bind bitfield 'bf.BF::b' to 'unsigned int&'

使用boost 1.57編譯並運行代碼(打印:[2016-09-19 20:21:33.018112] [0x000007fd1d5be672] [info]信息嚴重性消息0)

注意:

  1. 的課程COUT可以處理這個問題(所以我認爲這不僅是向後兼容的問題,而是一個bug)
  2. 提升1.61可以處理常量位字段,例如BOOST_LOG_TRIVIAL(info) << "An informational severity message " << BF().b;

我正在尋找解決方法。 建議?

回答

0

我找到了一個解決辦法 - 超載運營< <所有無符號整數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

最簡單的解決方法是將位域轉換爲完整整數。你可以做到這一點與鑄造:

BOOST_LOG_TRIVIAL(info) << "An informational severity message " 
    << static_cast< unsigned int >(BF().b); 
+0

我猜目前沒有解決方案通過boost :: log本身,對吧? 是否有計劃在未來修復它? –

+0

除非你想修補Boost.Log,否則沒有透明的解決方案。 https://svn.boost.org/trac/boost/ticket/11998與您的問題具有相同的來源,您也可以將您的案例添加到該故障單。 –

+0

如果你有寵物,我會非常高興擁有它。 TNX! –