Effective use of C++ iomanip library
Only std::setw()
is temporary. The other two calls, setiosflags
, and setprecision
have a permanent effect.
So, you could change your code to :
std::ostream& operator<<(std::ostream &output, const Vector &v){
output<<"["
<<std::setiosflags(std::ios::right | std::ios::scientific)
<<std::setw(23)
<<std::setprecision(16)
<<v._x<<", "
<<std::setw(23)
<<v._y<<", "
<<std::setw(23)
<<v._z<<"]";
return output;
}
But now you've borked the flags and precision for the next guy. Try this instead:
std::ostream& operator<<(std::ostream &output, const Vector &v){
std::ios_base::fmtflags f = output.flags(std::ios::right | std::ios::scientific);
std::streamsize p = output.precision(16);
output<<"["
<<std::setw(23)
<<v._x<<", "
<<std::setw(23)
<<v._y<<", "
<<std::setw(23)
<<v._z<<"]";
output.flags(f);
output.precision(p);
return output;
}
Finally, if you absolutely have to get rid of the duplication of the constant 23
, you could do something like this (but I wouldn't recommend it):
struct width {
int w;
width(int w) : w(w) {}
friend std::ostream& operator<<(std::ostream&os, const width& w) {
return os << std::setw(width.w);
}
};
std::ostream& operator<<(std::ostream &output, const Vector &v){
std::ios_base::fmtflags f = output.flags(std::ios::right | std::ios::scientific);
std::streamsize p = output.precision(16);
width w(23);
output<<"["
<<w
<<v._x<<", "
<<w
<<v._y<<", "
<<w
<<v._z<<"]";
output.flags(f);
output.precision(p);
return output;
}
See also this other question, where they decided that you can't make width permanent.
In C++20 you'll be able to do:
std::ostream& operator<<(std::ostream& output, const Vector& v){
const int width = 23, precision = 16;
return output << std::format(
"[{0:{3}.{4}e}, {1:{3}.{4}e}, {2:{3}.{4}e}]",
v._x, v._y, v._z, width, precision);
}
Unlike I/O manipulators, std::format
will not change ostream
's formatting state saving your from the problem mentioned by Bo Persson:
Your real problem is what happens to the next output after this one...
It will also work I/O manipulators work correctly with Vector
.
Until std::format
is available you can use the {fmt} library it is based on.
Everything but the setw() actually do that already. They are "sticky".
Your real problem is what happens to the next output after this one...