News for hackers. Stuff that matters.
Re:Std::string half of all allocations in the Chrome browser... by humanrebar on Friday December 05, 2014 @01:22PM (#8705934)
No, it's bigger than just copy parameters. std::string, even as a const reference, only works well in these cases:

1. your users already have a std::string

2. you want to support string literals

3. you'll be converting to string in the implementation anyway

What if I have a vector<const char>? What if I have a type called SanitizedString or OracleString? What about character arrays retrieved from C ABI calls?

And even if 3. is true, you are encoding implementation details in your interface. If you need to optimize your type later to use a trie, you'll have to do extra allocations and copies after all.

My point is that interfaces should require what they need: a character sequence if they need a character sequence and a character buffer if they need a character buffer. Hence my comment about std::string being more appropriately named std::string_buffer.

Even string_ref isn't perfect because it assumes your sequence is in contiguous memory. I'm hoping developments in the C++ language and standard library comes up with a good answer to that problem, perhaps one that leverages the concepts proposals. But even a concepts-based solution will have drawbacks, such as requiring even more code to go in your header file.

There are no comments on this comment yet.