Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
Last active January 3, 2016 11:38
Show Gist options
  • Save dgodfrey206/8457070 to your computer and use it in GitHub Desktop.
Save dgodfrey206/8457070 to your computer and use it in GitHub Desktop.
A manipulator that allows you to specify how many new lines you want written to the output stream at one time.
#include <iostream>
class endl_n_manipulator
{
public:
endl_n_manipulator(int count) : count(count) { }
friend std::ostream& operator<<(std::ostream& os, const endl_n_manipulator& manip)
{
std::ostream::sentry s(os);
std::ios_base::iostate err = std::ios_base::goodbit;
if (s)
{
try
{
std::ostreambuf_iterator<char> out(os);
std::fill_n(out, manip.count, '\n');
if (out.failed())
os.setstate(std::ios_base::badbit);
} catch (...) { os.setstate(std::ios_base::badbit); }
} else
err |= std::ios_base::failbit;
if (err)
os.setstate(err);
return os;
}
private:
int count;
};
endl_n_manipulator endl_n(int count)
{
return endl_n_manipulator(count);
}
int main()
{
std::cout << "Hello" << endl_n(3) << "World";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment