on Wed Jul 17 2013, Daryle Walker
Date: Wed, 17 Jul 2013 22:09:08 -0500 From: plong@packetizer.com
For example, the following snippet prints "hello, goodbye".
char c[BUFSIZ] = "hello"; istringstream iss(c); strcpy(c, "goodbye"); cout << iss.str() << ", " << c;
However, the library I'm working on doesn't copy. Instead, pointers are passed in and out. So, for example, this code prints "goodbye, goodbye".
char c[BUFSIZ] = "hello"; ibitstream ibs(c); strcpy(c, "goodbye"); cout << ibs.data() << ", " << c;
I don't think either way is more correct than the other. The reason I don't copy is for memory and CPU performance, thinking that the streams a bit-stream library typically processes are larger than the streams that a string-stream library processes and are more time critical. I very well may be mistaken, though. Anyway, what do you people think? Copy or don't copy? Maybe I should do copies just to follow the semantics of std::stringstream.
I would suggest copying.
But when the source is an rvalue, move instead. That handles the efficiency issues. -- Dave Abrahams