2015-10-02 15 views
5

Im Boost.Log documentation, es wird gesagt, dassRichtig Betreiber überlasten << in Boost.Log

Hinweis

Die Bibliothek verwendet basic_formatting_ostream Stream-Typ für Plattenformatierung, so dass, wenn Attributwert Anpassen der Formatierung Regeln die operator<< müssen basic_formatting_ostream statt std::ostream verwenden.

jedoch in der Dokumentation, alles was ich sehe ist eine Überlastung operator << auf std::ostream statt basic_formatting_ostream im Beispielcode. Sehen Sie sich beispielsweise die Überladung für den benutzerdefinierten Typ severity_levelhere an.

Nach meinen Tests funktionierten die Überladungen auf std::ostream und basic_formatting_ostream beide gut. Also, ich frage mich, was sind die Vorteile der Überladung auf der einen statt der anderen.

+1

Der Vorteil der Verwendung von 'std :: ostream' sollte ziemlich offensichtlich sein: Sie können es auch für" normale "Ausgabe verwenden. :) –

Antwort

3

Es gibt keine Probleme, nur operator << (std::ostream&, ...) in Überlastung, da 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; 
} 

wo stream() kehrt std::ostream& hat. Wenn Sie operator << mit dem ersten Argument formatting_ostream überladen, dann kann dieses nur mit boost::log verwendet werden, wenn Sie für std::ostream& überladen, dann kann dies für boost::log und für einen anderen Ausgang verwendet werden.

Zitat von Header-Datei:

* 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. 
+0

Aber das Einfügen in 'basic_formatting_ostream' scheint mehr Arbeit zu machen als das Einfügen in' std :: ostream'. Siehe zum Beispiel den überladenen 'Operator <<', den 'basic_formatting_ostream' für' const char * 'vorgesehen hat. – Lingxi

+0

@Lingxi nope, 'basic_formatting_ostream' implementieren so einfach wie möglich die Schnittstelle von' basic_ostream'. – ForEveR

+0

Also, die Boost.Log Dokumentation ist gerade veraltet oder was? – Lingxi

0

Wenn Sie nur operator<<(std::ostream) überlasten, wird es für jeden Stream-Ausgabe arbeiten, einschließlich der zu basic_formatting_ostream. Wenn Sie nur operator<<(basic_formatting_ostream) überladen, funktioniert es nur für die Ausgabe zu diesem Typ des Stromes.

Sie möchten jedoch möglicherweise überladen beide, zum Beispiel, wenn Sie verschiedene oder mehr Informationen (wie die Adresse eines Objekts) zum Protokoll bereitstellen möchten.