C++ – how does ofstream or ostream type cast all types to string


any system defined user type past to ostream object is converted to a string or char* ?

like cout<<4<<"Hello World";

works perfectly fine, how is this achieved? is the << operator overloaded for each and every type? is there a way to achieve it through just one generic overloaded function? what i mean is can i have just one overloaded operator method with one parameter(like void*) and then decide inside that method how to typecast integer to char*

Things worked partially if i overload operator << using Template i.e

class UIStream
 ofstream stream;
 template <typename T>
 UIStream& operator << (const T);

so this works

 UIStream my_stream;

however it gives compiler error when i do this

my_stream <<endl;

error C2678: binary '<<' : no operator found which takes a left-hand operand of type 'UIStream' (or there is no acceptable conversion)

Is not std::endl a type of object too?

Best Solution

After re-reading your question (as a result of a comment in this answer) I have realized that what you want is not only conversions into string (my assumptions in the other answer here), but rather forwarding into the internal ofstream.

Now, what you want to achieve is not simple, and may be overkill in most cases. In the implementation of [make_string][3] that I have (that forwards to an internal ostringstream), I don't allow for manipulators to be passed. If the user wants to add a new line (we develop under linux) they just pass a '\n' character.

Your problem is forwarding manipulators (std::hex, std::endl...). Your operator<< is defined as taking a constant instance of a type T, but manipulators are function pointers and the compiler is not able to match it against your methods.

Manipulators are functions that operate on the std::basic_ostream template. The basic_ostream template and ostream class are defined as:

template <typename TChar, typename TTraits = char_traits<TChar> >
class basic_ostream;

typedef basic_ostream<char> ostream;
// or
// typedef basic_ostream<wchar_t> if using wide characters

Then the possible manipulators that can be passed to a std::ostream are:

typedef std::ostream& (*manip1)( std::ostream& );

typedef std::basic_ios< std::ostream::char_type, std::ostream::traits_type > ios_type;
typedef ios_type& (*manip2)( ios_type& );

typedef std::ios_base& (*manip3)( std::ios_base& );

If you want to accept manipulators you must provide that overload in your class:

class mystream
   template <typename T> 
   mystream& operator<<( T datum ) {
      stream << datum;
      return *this
   // overload for manipulators
   mystream& operator<<( manip1 fp ) {
      stream << fp;
      return *this;
   mystream& operator<<( manip2 fp ) {
      stream << fp;
      return *this;
   mystream& operator<<( manip3 fp ) {
      stream << fp;
      return *this;

In particular, the signature for endl (which may be the only one you require) is:

template <typename Char, typename Traits>
   std::endl( std::basic_ostream<Char,Traits>& stream );

so it falls under the manip1 type of functions. Others, like std::hex fall under different categories (manip3 in this particular case)