Skip to content

Instantly share code, notes, and snippets.

@dblalock
Created July 11, 2014 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblalock/93a98b481e16baeea2be to your computer and use it in GitHub Desktop.
Save dblalock/93a98b481e16baeea2be to your computer and use it in GitHub Desktop.
C++ formatted width output stream
class formatted_output
{
private:
int width;
ostream& stream_obj;
public:
formatted_output(ostream& obj, int w): width(w), stream_obj(obj) {}
template<typename T>
formatted_output& operator<<(const T& output)
{
stream_obj << setw(width) << output;
return *this;
}
formatted_output& operator<<(ostream& (*func)(ostream&))
{
func(stream_obj);
return *this;
}
};
You can now call it like the following:
formatted_output field_output(cout, 10);
field_output << x << y << endl;
//http://stackoverflow.com/a/7248697
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment