The answer to “good practice” is difficult. I would say, in general, not a good practice, but for some specific use cases it is a very good practice. It all depends on how well you can trust the client throughout the life of the provided memory. In general: no trust. In special cases: OK.
There is one pretty important thing to consider performance:
Do you plan to use an implicit exchange (with a copy for recording and counting links) of your selected variants of your lines? Or do you plan to use the semantics of values (always copy, not reference)?
In multiprocessor and multithreaded environments, semantics are the preferred way for strings. Increased productivity through the use of implicit sharing is destroyed by the necessary blocking in multi-threaded environments.
Please note that your sentence may make sense even with multithreading: you can use copy-on-write perfectly when moving from your external memory to the selected option (without the need for locking) and the semantics of values from now on (also no locking is necessary). That would be better.
I would suggest that your option works well in very specific use cases, for example, when you copied memory to some file with a lot of lines, and you do not want to store copies of these small lines and fragment memory.
However, in the general case, I would not bother and just use std::string .
source share