|
![](/i/fill.gif) |
Darren New <dne### [at] san rr com> wrote:
> Yeah. I'd just been thinking about Warp's example earlier that needed
> "weak" references to actually not "leak" resources. I was trying to
> figure out how C++ handled it, if at all.
Stream manipulation flags are one thing which is shamefully bad designed
in C++, and I really don't understand what they were thinking of.
A few flags are set only until the next output action which is affected
by that flag (after which it's automatically reset). However, the rest are
permanently set until they are explicitly reset. This means that these
flags can "leak". For example this may well happen:
std::cout << 10 << std::endl;
foo();
std::cout << 10 << std::endl;
If foo() calls, for example, std::hex(std::cout), then what is printed is:
10
a
foo() could alleviate this problem by storing the flags into a variable
at the beginning and then restoring those flogs when it ends, but as we
all know, functions can have surprising exit points. (It's still possible
to make sure that the flags are restored at any exit point by using the
destructor mechanism of C++, but it becomes a bit more laborious. I'm
actually surprised the standard library doesn't offer an class type for
this exact purpose...)
--
- Warp
Post a reply to this message
|
![](/i/fill.gif) |