助推日志的问题,版本1.59

Problems with boost log, version 1.59

本文关键字:版本 问题 日志      更新时间:2023-10-16

下面的代码在boost 1.57下正常工作:

#include <iostream>
#include <boost/log/trivial.hpp>
struct Foo
{
    int d=1;
};
std::ostream& operator<<(std::ostream& out, const Foo& foo)
{
    out << "Foo: " << foo.d;
    return out;
}
int main()
{
    BOOST_LOG_TRIVIAL(info) << Foo();
    return EXIT_SUCCESS;
}

与boost 1.59相同的代码失败。第一个gcc错误信息是:

error: no match for ' operator<<'(操作数类型为' boost::log::v2s_mt_posix::basic_record_ostream ' and ' Foo ')

文档和发行说明都没有说明需要更改的内容。

现场版看起来问题是在enable_if_formatting_ostream结构。它是在这次提交中添加的。看起来像

template< typename StreamT, typename R >
struct enable_if_formatting_ostream {};
template< typename CharT, typename TraitsT, typename AllocatorT, typename R >
struct enable_if_formatting_ostream< basic_formatting_ostream< CharT, TraitsT, AllocatorT >, R > { typedef R type; };

现在operator <<

template< typename StreamT, typename T >
inline typename boost::log::aux::enable_if_formatting_ostream< StreamT, StreamT& >::type
operator<< (StreamT& strm, T const& value)

以前是

template< typename CharT, typename TraitsT, typename AllocatorT, typename T >
inline basic_formatting_ostream< CharT, TraitsT, AllocatorT >&
operator<< (basic_formatting_ostream< CharT, TraitsT, AllocatorT >& strm, T const& value)

并且由于record_ostream派生自formatting_ostream编译器可以找到过载,但现在没有,因为使用了SFINAE,并且只有在使用formatting_ostream时结构才会具有type类型定义。这是一种变通方法