Streaming Operators



About

Code snippets for various uses of operator<<.


Printing


Templatised
class Foo {
public:
template<typename OutputStream>
friend OutputStream& operator<<(OutputStream &ostream, const Foo &foo);
};
template<typename OutputStream>
OutputStream& operator<<(OutputStream &os, const Foo &foo)
{
  os << "This is foo\n";
  return os;
}


Std Ostream
# foo.hpp
#include <ostream>
class Foo {
public:
  friend std::ostream& operator<<(std::ostream &os, const Foo &foo);
};
# foo.cpp 
std::ostream& operator<< (std::ostream &os, const Foo &foo) {
  os << "This is foo\n";
  return os;
}

Finding

Always Include the Header

Quite often you can run into trouble having it drop into another streaming operator's function and give you wierd errors because another template tries to match it. Make sure you include your header so that it finds yours!

Too many operators spoiling the broth

Sometimes you can have trouble getting your streaming operator priority. In this case, call it manually:

dslam_common::operator>>(d, graph);