3 ms·
What’s the ownership story for string views?
by saagarjha 4y ago
What’s the ownership story for string views?
- eco 4y agoThey don't own anything. It's just a pointer and length. They don't allocate/deallocate.
- saagarjha 4y agoI mean clearly something needs to own the buffer for a new string.
- tialaramex 4y agoSure, but that's not the string_view's problem, you can't just make string_views, the string you want to borrow a view into needs to exist first. Imagine you go to a library and insist on borrowing "My Cousin Rachel", but they don't have it. "Oh I don't care whether you have the book, I just want to borrow it" is clearly nonsense. If they don't have it, you can't borrow it.
- saagarjha 4y agoWalter is talking about D, and he said this: > D doesn't distinguish between a string and a string view. In C++ std::string owns the buffer and std::string_view borrows it. If there is no difference between the two in D, then how is this difference bridged?
- WalterBright 4y agoYou can use automatic memory management and not worry about it. Or you can use D's prototype ownership/borrowing system. Or you can encapsulate them in something that manages the memory. Or you can do ownership/borrowing by convention (it's not hard to do).
- saagarjha 4y agoAutomatic memory management makes copies?
- WalterBright 4y agoNo. Another word for automatic memory management is garbage collection.
- saagarjha 4y agoI guess I should rephrase. Let's say I have a string, which owns its buffer. What happens in D if I take a substring of it? Does a copy of that section occur to form a new string?