[mlpack-svn] [MLPACK] #319: operator<<(ostream&, const T&): is it possible without C++11?

MLPACK Trac trac at coffeetalk-1.cc.gatech.edu
Tue Jan 28 16:56:19 EST 2014


#319: operator<<(ostream&, const T&): is it possible without C++11?
---------------------+------------------------------------------------------
 Reporter:  rcurtin  |        Owner:                                                 
     Type:  defect   |       Status:  new                                            
 Priority:  trivial  |    Milestone:                                                 
Component:  mlpack   |     Keywords:  c++11, tostring, cli, cout, ostream, operator<<
 Blocking:           |   Blocked By:                                                 
---------------------+------------------------------------------------------
 The following code allows me to print any mlpack object that has a
 ToString() method to cout (or other ostream):

 {{{
  */
  * Allow ostream::operator<<(T&) for mlpack objects that have ToString
  * implemented.
  */
 #ifndef __MLPACK_CORE_UTIL_OSTREAM_EXTRA_HPP
 #define __MLPACK_CORE_UTIL_OSTREAM_EXTRA_HPP

 #include "sfinae_utility.hpp"

 // Hide in a namespace so we don't pollute the global namespace.
 namespace mlpack {
 namespace util {
 namespace misc {

 HAS_MEM_FUNC(ToString, HasToString)

 }; // namespace misc
 }; // namespace util
 }; // namespace mlpack

 template<
     typename T,
     typename junk1 = typename boost::enable_if<boost::is_class<T> >::type,
     typename junk2 = typename
 boost::enable_if<mlpack::util::misc::HasToString<T, std::string(T::*)()
 const> >::type
 >
 std::ostream& operator<<(std::ostream& stream,
                          const T& t)
 {
   stream << t.ToString();
   return stream;
 }

 #endif
 }}}

 But it only works with the C++11 standard.  Seeing as how mlpack doesn't
 force support for that (yet), is there a way to get the same functionality
 without C++11?  Personally I don't think it's possible, but I thought I
 would put it out here in case someone else is interested.  Once C++11
 support is forced for mlpack (whenever that may be), this ticket can be
 closed because it'll be irrelevant.

-- 
Ticket URL: <http://trac.research.cc.gatech.edu/fastlab/ticket/319>
MLPACK <www.fast-lab.org>
MLPACK is an intuitive, fast, and scalable C++ machine learning library developed at Georgia Tech.


More information about the mlpack-svn mailing list