   Forcing C++ streams to flush to the disk
  I have made a nice logging class which derives from basic_ofstream. However if I use it then I have to manually flush - I want everything to be flushed when operator<< is called; it's not good that a user has to know to flush after logging a message.Is it possible to somehow turn on 'autoflush'? Or do I have to ovveride every single version of operator<< which from my MSDN is these... a real chore!    basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));    basic_ostream& operator<<(basic_ios<E, T>& (*pf)(basic_ios<E, T>&));    basic_ostream& operator<<(ios_base<E, T>& (*pf)(ios_base<E, T>&));    basic_ostream& operator<<(basic_streambuf<E, T> *sb);    basic_ostream& operator<<(const char *s);    basic_ostream& operator<<(char c);    basic_ostream& operator<<(bool n);    basic_ostream& operator<<(short n);    basic_ostream& operator<<(unsigned short n);    basic_ostream& operator<<(int n);    basic_ostream& operator<<(unsigned int n);    basic_ostream& operator<<(long n);    basic_ostream& operator<<(unsigned long n);    basic_ostream& operator<<(float n);    basic_ostream& operator<<(double n);    basic_ostream& operator<<(long double n);    basic_ostream& operator<<(void * n);
  Of course there is! cerr, for example, is unbuffered. Just set the unitbuf flag :)   ;  I would believe buffering after each operator >> call might affect performance. Logging would usually appear as follows:log << "X = " << x << ", Y = " << y << "\n";You'd want the log to flush to file only after the entire statement has occured (and thus all writing is done). In order to work around this, you could wrap the ostream inside a small object, forwarding all operator<< to it, and have the wrapper's destructor flush only after all temporary wrappers have been destroyed (thus marking the end of a line). That would have the annoying side-effect of breaking the expected behavior of operator<< returning an ostream&. Another way would be to append an std::endl or std::flush after each line, either manually or using a macro. ;  As ToohrVyk says, std::endl and std::flush always cause a flush of the stream.  Using "\n" does not.  So stick those in your class wherever you think it would be appropriate.   