News for hackers. Stuff that matters.
Re:Care is needed to use C++ std:optional with non-trivial o... by usefulcat on Friday January 13, 2023 @09:18AM (#34368036)
In this example, you don't need to know how things (i.e. std::string) are implemented internally so much as you need to know whether or not they're expensive to copy. That's a pretty fundamental aspect of the language, if you care about performance.

If you do care and know about the particular implementation, I guess you might counter by saying that a COW string would be cheap(ish) to copy, or ditto if the string is short and the implementation uses the small string optimization. But at a higher level it's a very good general rule to just avoid copying anything unless you know for sure that it's small and doesn't use dynamic allocation.

There are no comments on this comment yet.