Something a lot of C++ programmers miss (read: me) when first using stringstream
s is the fact that the copy returned by stringstream::str()
is a temporary, which lasts until the end of the expression it's used in. However, I don't understand:
- How this is done. Looking in the
sstream
header for libstdc++, I only see a copy being made and returned. How is the lifetime restricted? - Why this is the desired behavior, especially since it is such a common gotcha. If a copy is being made anyway, why can't I take ownership of it?
Note that this is not a duplicate of stringstream, string, and char* conversion confusion. That goes over explaining the behavior and workarounds; I'm looking for mechanics and rationale.