3 ms·
Maybe a C++ person can help me out, I am staring at this C++ translation of Rust's elision rules: > If there are multiple input lifetimes but one of them appli
by tialaramex 5y ago
Maybe a C++ person can help me out, I am staring at this C++ translation of Rust's elision rules:
> If there are multiple input lifetimes but one of them applies to the implicit this parameter, that lifetime is assigned to all elided output lifetimes.
In Rust we have self rather than this in methods, but importantly we sometimes don't take a reference here, and that's still a method, and you still have the self parameter, but the lifetime elision rules don't apply. They don't apply because if you've actually got self, not some reference to self, the lifetime of self is going to end when the function finishes, so returning things with that lifetime is nonsense.
This can make sense in Rust for transformative methods. If there's a method on a God that turns them into a Mortal, the God doesn't exist any more when that method exits, the Mortal is the return type, and if you just drop it then I guess sucks to be them. (In Rust, and I think in C++ you can label something as must-use and cause a warning or error if the programmer forgets to use it).
It seems though, as if this C++ rule would hit such methods too. Can you distinguish in C++ between "a reference to me" and "me" when it comes to methods? If you had a method on a God class in C++ that transforms the God into a Mortal and returns the Mortal, what does that look like? Does this elision rule make sense to you for such a scenario?
- Rusky 5y agoC++ does not currently have such methods- all `this` parameters are always by (some kind of) reference. (This doesn't stop you from moving out of `*this`, but C++ moves require the source object to remain valid, and I'm not sure I've ever seen that in the wild anyway!) This will change in C++23 with "explicit this", but then the `this` parameter will no longer be implicit and that translated elision rule will no longer apply either. So in today's C++ your example might look something like this: struct God { Mortal transform() && { // Take an rvalue reference for `*this`, I guess? return Mortal{ /* Move some fields out of `*this` probably? */ }; } }; The elision rule does apply here even though we're moving out of `*this`, but again we are still taking a reference and the God object remains valid afterward anyway. With "explicit this" you could instead write something like this: struct God { Mortal transform(this God self) { ... } }; Now there's no reference and the rule ceases to apply. (Though in most cases you are still going to be leaving a valid, but now at least unrelated, God object behind- the only way to avoid that is to construct it directly in argument position so it stays a prvalue.)
- enedil 5y agoIn our codebase though we have some places where we do `delete this;` and later use placement new, to restore `this` to some valid state.
- Rusky 5y agoThat only seems tangentially related to me, but it is interesting, haha. Specifically that sounds like it would cause use-after-frees- if you actually `delete this` and then `new (this) Bla`, you're writing to memory that's already been returned to the allocator. Maybe you meant `this->~Bla()` rather than `delete this`? Or a custom `operator delete`?