Jonathan Wakely 3d13bd
From 7da193fde1a9c1bc925ee980339f4df2e1a66fa7 Mon Sep 17 00:00:00 2001
Jonathan Wakely 3d13bd
From: Andrey Semashev <andrey.semashev@gmail.com>
Jonathan Wakely 3d13bd
Date: Sun, 23 Aug 2015 17:27:20 +0300
Jonathan Wakely 3d13bd
Subject: [PATCH] Fixed compilation of operator<< into a record ostream, when
Jonathan Wakely 3d13bd
 the operator right hand argument is not directly supported by
Jonathan Wakely 3d13bd
 formatting_ostream. Fixed #11549.
Jonathan Wakely 3d13bd
Jonathan Wakely 3d13bd
---
Jonathan Wakely 3d13bd
Jonathan Wakely 3d13bd
diff --git a/include/boost/log/sources/record_ostream.hpp b/include/boost/log/sources/record_ostream.hpp
Jonathan Wakely 3d13bd
index b3c58e2..c1e8059 100644
Jonathan Wakely 3d13bd
--- a/include/boost/log/sources/record_ostream.hpp
Jonathan Wakely 3d13bd
+++ b/include/boost/log/sources/record_ostream.hpp
Jonathan Wakely 3d13bd
@@ -39,6 +39,18 @@ namespace boost {
Jonathan Wakely 3d13bd
 
Jonathan Wakely 3d13bd
 BOOST_LOG_OPEN_NAMESPACE
Jonathan Wakely 3d13bd
 
Jonathan Wakely 3d13bd
+template< typename CharT >
Jonathan Wakely 3d13bd
+class basic_record_ostream;
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+namespace aux {
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+template< typename StreamT, typename R >
Jonathan Wakely 3d13bd
+struct enable_if_record_ostream {};
Jonathan Wakely 3d13bd
+template< typename CharT, typename R >
Jonathan Wakely 3d13bd
+struct enable_if_record_ostream< basic_record_ostream< CharT >, R > { typedef R type; };
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+} // namespace aux
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
 /*!
Jonathan Wakely 3d13bd
  * \brief Logging record adapter with a streaming capability
Jonathan Wakely 3d13bd
  *
Jonathan Wakely 3d13bd
@@ -174,6 +186,55 @@ typedef basic_record_ostream< char > record_ostream;        //!< Convenience typ
Jonathan Wakely 3d13bd
 typedef basic_record_ostream< wchar_t > wrecord_ostream;    //!< Convenience typedef for wide-character logging
Jonathan Wakely 3d13bd
 #endif
Jonathan Wakely 3d13bd
 
Jonathan Wakely 3d13bd
+// Implementation note: these operators below should be the least attractive for the compiler
Jonathan Wakely 3d13bd
+// so that user's overloads are chosen, when present. We use function template partial ordering for this purpose.
Jonathan Wakely 3d13bd
+// We also don't use perfect forwarding for the right hand argument because in ths case the generic overload
Jonathan Wakely 3d13bd
+// would be more preferred than the typical one written by users:
Jonathan Wakely 3d13bd
+//
Jonathan Wakely 3d13bd
+// record_ostream& operator<< (record_ostream& strm, my_type const& arg);
Jonathan Wakely 3d13bd
+//
Jonathan Wakely 3d13bd
+// This is because my_type rvalues require adding const to the type, which counts as a conversion that is not required
Jonathan Wakely 3d13bd
+// if there is a perfect forwarding overload.
Jonathan Wakely 3d13bd
+template< typename StreamT, typename T >
Jonathan Wakely 3d13bd
+inline typename boost::log::aux::enable_if_record_ostream< StreamT, StreamT& >::type
Jonathan Wakely 3d13bd
+operator<< (StreamT& strm, T const& value)
Jonathan Wakely 3d13bd
+{
Jonathan Wakely 3d13bd
+    typedef basic_formatting_ostream< typename StreamT::char_type > formatting_ostream_type;
Jonathan Wakely 3d13bd
+    static_cast< formatting_ostream_type& >(strm) << value;
Jonathan Wakely 3d13bd
+    return strm;
Jonathan Wakely 3d13bd
+}
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+template< typename StreamT, typename T >
Jonathan Wakely 3d13bd
+inline typename boost::log::aux::enable_if_record_ostream< StreamT, StreamT& >::type
Jonathan Wakely 3d13bd
+operator<< (StreamT& strm, T& value)
Jonathan Wakely 3d13bd
+{
Jonathan Wakely 3d13bd
+    typedef basic_formatting_ostream< typename StreamT::char_type > formatting_ostream_type;
Jonathan Wakely 3d13bd
+    static_cast< formatting_ostream_type& >(strm) << value;
Jonathan Wakely 3d13bd
+    return strm;
Jonathan Wakely 3d13bd
+}
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+template< typename StreamT, typename T >
Jonathan Wakely 3d13bd
+inline typename boost::log::aux::enable_if_record_ostream< StreamT, StreamT& >::type
Jonathan Wakely 3d13bd
+operator<< (StreamT&& strm, T const& value)
Jonathan Wakely 3d13bd
+{
Jonathan Wakely 3d13bd
+    typedef basic_formatting_ostream< typename StreamT::char_type > formatting_ostream_type;
Jonathan Wakely 3d13bd
+    static_cast< formatting_ostream_type& >(strm) << value;
Jonathan Wakely 3d13bd
+    return strm;
Jonathan Wakely 3d13bd
+}
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+template< typename StreamT, typename T >
Jonathan Wakely 3d13bd
+inline typename boost::log::aux::enable_if_record_ostream< StreamT, StreamT& >::type
Jonathan Wakely 3d13bd
+operator<< (StreamT&& strm, T& value)
Jonathan Wakely 3d13bd
+{
Jonathan Wakely 3d13bd
+    typedef basic_formatting_ostream< typename StreamT::char_type > formatting_ostream_type;
Jonathan Wakely 3d13bd
+    static_cast< formatting_ostream_type& >(strm) << value;
Jonathan Wakely 3d13bd
+    return strm;
Jonathan Wakely 3d13bd
+}
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
+#endif // !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
Jonathan Wakely 3d13bd
+
Jonathan Wakely 3d13bd
 namespace aux {
Jonathan Wakely 3d13bd
 
Jonathan Wakely 3d13bd
 //! Internal class that provides formatting streams for record pumps
Jonathan Wakely 3d13bd
diff --git a/include/boost/log/utility/formatting_ostream.hpp b/include/boost/log/utility/formatting_ostream.hpp
Jonathan Wakely 3d13bd
index 4345206..744acc0 100644
Jonathan Wakely 3d13bd
--- a/include/boost/log/utility/formatting_ostream.hpp
Jonathan Wakely 3d13bd
+++ b/include/boost/log/utility/formatting_ostream.hpp
Jonathan Wakely 3d13bd
@@ -779,6 +779,13 @@ void basic_formatting_ostream< CharT, TraitsT, AllocatorT >::aligned_write(const
Jonathan Wakely 3d13bd
 
Jonathan Wakely 3d13bd
 // Implementation note: these operators below should be the least attractive for the compiler
Jonathan Wakely 3d13bd
 // so that user's overloads are chosen, when present. We use function template partial ordering for this purpose.
Jonathan Wakely 3d13bd
+// We also don't use perfect forwarding for the right hand argument because in ths case the generic overload
Jonathan Wakely 3d13bd
+// would be more preferred than the typical one written by users:
Jonathan Wakely 3d13bd
+//
Jonathan Wakely 3d13bd
+// formatting_ostream& operator<< (formatting_ostream& strm, my_type const& arg);
Jonathan Wakely 3d13bd
+//
Jonathan Wakely 3d13bd
+// This is because my_type rvalues require adding const to the type, which counts as a conversion that is not required
Jonathan Wakely 3d13bd
+// if there is a perfect forwarding overload.
Jonathan Wakely 3d13bd
 template< typename StreamT, typename T >
Jonathan Wakely 3d13bd
 inline typename boost::log::aux::enable_if_formatting_ostream< StreamT, StreamT& >::type
Jonathan Wakely 3d13bd
 operator<< (StreamT& strm, T const& value)