Search code examples
c++compiler-errorsbackwards-compatibilitybit-fieldsboost-log

boost log non-const bitfield compilation error (backward compatibility issue)


I took the example from http://www.boost.org/doc/libs/1_61_0/libs/log/example/doc/tutorial_trivial_flt.cpp and added a bitfield print:

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

With boost 1.61 I got a compilation error:

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

With boost 1.57 the code is compiled and run (prints: [2016-09-19 20:21:33.018112] [0x000007fd1d5be672] [info] An informational severity message 0)

notice that:

  1. cout of-course can handle this (so I think it is not just a backward compatibility issue, but a bug)
  2. boost 1.61 can handle const bitfield, e.g BOOST_LOG_TRIVIAL(info) << "An informational severity message " << BF().b;

I'm searching for workaround. Suggestions?


Solution

  • I found a workaround - overloading operator<< for record_ostream of all unsigned integers:

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

    The integers got copied (are taken by value) so there is no bind issue