正确重载operator&LT;&LT;在Boost.Log [英] Properly overload operator << in Boost.Log
问题描述
在<一个href=\"http://www.boost.org/doc/libs/1_59_0/libs/log/doc/html/log/detailed/utilities.html#log.detailed.utilities.manipulators.to_log\"相对=nofollow> Boost.Log文档,据说
注意
库使用 basic_formatting_ostream
流类型的记录格式,所以定制属性值的格式时,
掌管运营商的LT;&LT;
必须使用 basic_formatting_ostream
而不是
的std :: ostream的
。
The library uses basic_formatting_ostream
stream type for record formatting, so when customizing attribute value formatting
rules the operator<<
must use basic_formatting_ostream
instead of
std::ostream
.
然而,在整个文档中,我看到的是超载运营商的LT;&LT;
在的std :: ostream的
而不是 basic_formatting_ostream
在这个例子code。例如,请参阅自定义类型的重载 severity_level
的这里。
However, throughout the documentation, all I see is overloading operator <<
on std::ostream
rather than basic_formatting_ostream
in the example code. For example, see the overload for the custom type severity_level
here.
根据我的测试,在的std :: ostream的
和重载 basic_formatting_ostream
都工作得很好。所以,我想知道什么是对超载而不是一个其他的优点。
According to my tests, the overloads on std::ostream
and basic_formatting_ostream
both worked fine. So, I'm wondering what are the advantages of overloading on one instead of the other.
推荐答案
有在超载只是运营商的LT没有问题;&LT; (性病:: ostream的和放,...)
,因为 formatting_ostream
的
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)
{
strm.stream() << value;
return strm;
}
其中,流()
收益的std :: ostream的&安培;
。如果你重载运营商的LT;&LT;
与第一个参数 formatting_ostream
,比这只能与使用的boost ::登录
,如果过载的std :: ostream的&安培;
,那么这可以用于提振::日志
和另一个输出。
where stream()
returns std::ostream&
. If you overload operator <<
with first arg formatting_ostream
, than this can be used only with boost::log
, if you overload for std::ostream&
, then this can be used for boost::log
and for another output.
这头文件中引用:
* This stream wrapper is used by the library for log record formatting. It implements the standard string stream interface
* with a few differences:
*
* \li It does not derive from standard types <tt>std::basic_ostream</tt>, <tt>std::basic_ios</tt> and <tt>std::ios_base</tt>,
* although it tries to implement their interfaces closely. There are a few small differences, mostly regarding <tt>rdbuf</tt>
* and <tt>str</tt> signatures, as well as the supported insertion operator overloads. The actual wrapped stream can be accessed
* through the <tt>stream</tt> methods.
* \li By default, \c bool values are formatted using alphabetical representation rather than numeric.
* \li The stream supports writing strings of character types different from the stream character type. The stream will perform
* character code conversion as needed using the imbued locale.
* \li The stream operates on an external string object rather than on the embedded one. The string can be attached or detached
* from the stream dynamically.
*
* Although <tt>basic_formatting_ostream</tt> does not derive from <tt>std::basic_ostream</tt>, users are not required to add
* special overloads of \c operator<< for it since the stream will by default reuse the operators for <tt>std::basic_ostream</tt>.
* However, one can define special overloads of \c operator<< for <tt>basic_formatting_ostream</tt> if a certain type needs
* special formatting when output to log.
这篇关于正确重载operator&LT;&LT;在Boost.Log的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!