http://www.cplusplus.com/reference/iostream/manipulators/
EDIT: Sorry, I was a little short handed. What I meant was to research how iomanip works, I know you are aware of sets(n) << ""
Using iomanip as a baseline for a quick and dirty implementation, this is what I came up with:
#include <iostream>
#include <iomanip>
#include <string>
class setpw_ {
int n_;
std::string s_;
public:
explicit setpw_(int n) : n_(n), s_("") {}
setpw_(int n, const std::string& s) : n_(n), s_(s) {}
template<typename classT, typename traitsT>
friend std::basic_ostream<classT, traitsT>&
operator<<(std::basic_ostream<classT, traitsT>& os_, const setpw_& x) {
os_.width(x.n_);
os_ << "";
if ( x.s_.length()){
os_ << x.s_;
}
return os_;
}
};
setpw_ setpw(int n) { return setpw_(n); }
setpw_ indent(int n, const std::string& s) { return setpw_(n, s); }
int
main(int argc, char** argv) {
std::cout << setpw(8) << "Hello, World" << std::endl;
std::cout << indent(8, "----^BYE^---") << std::endl;
return 0;
}