My original response was not favorable. Alas, we are on the internet, and after re-reading your post I don’t think you are being deliberately rude. But your phrasing is rather aggressive.
Be aware, you may not care, but if you do it the way you are championing then you should not be surprised when things go sideways. The compiler can be very aggressive with optimizations, meaning that your raw data access may very well go out of scope before
std::cout
gets it. In the case of the strings it is unlikely to be an issue (assuming either lack of threads or proper synchronization).
Duthomhas, that may be robust it uses up a lot of resources. Would you code it the same way for 10 million user names? |
Obviously not. I would not code it
anything like that. I would design a better data structure than the one I was responding to.
But, were I absolutely required to design it poorly, and not require the user to call
.c_str()
on the individual components themselves, I’m sure I could manage some limited way to avoid eating up memory. A simple circular buffer of N strings may suffice.
The example was designed to highlight the difference between owned data and referenced data. But if that is not what people are getting out of it maybe I should strike it out.
Streams can do conversions. Do you like to play ping-pong conversion with the built-in methods or with streams and why? |
Again, streams are
designed for serialization. This is a pretty explicit concept in the streams library.
But whether the actual serialization/conversion/whatever you want to call it occurs using a stringstream or not is irrelevant. If it matters, profile the different methods apropos to your code and choose the one that works best. Otherwise use the most convenient.
Chances are, however, that the differences underneath, if any, are minor — for built-in data types at least. User code is a separate matter. Profile.
Given a choice, though, I won’t ping-pong anything. That’s a waste of time and resources.
The whole point of my original response was just to point out that you are working with a design flaw. While it worked for you (and as yet I am unsure you recognize why it is a problem), I absolutely disagree with trying to learn to build something using a broken design.
Do it all correctly, or you simply waste time doing one part as correctly as you can get it while the rest of it is just wrong. The consequence is that you have learned incorrectly on, IMHO, a significant level.
$0.02