|
|
Subscribe / Log in / New account

Ownership and lifetimes

Ownership and lifetimes

Posted Jul 14, 2021 12:44 UTC (Wed) by khim (subscriber, #9252)
In reply to: Ownership and lifetimes by mathstuf
Parent article: Announcing Arti, a pure-Rust Tor implementation (Tor blog)

> In that example, `p` cannot be destroyed.

True, sorry I wasn't clear. Object owned by p may be destroyed or not destroyed, p may be usable or unusable, etc (I hope `p` just means p in your message and not some new footgun C++ gained while I wasn't looking).

> Rust can end `p`'s lifetime at the `foo` call, but C++ has lifetimes go to the end of the scope no matter what because there's no mechanism to say "`p` no longer exists and is now `foo`'s problem".

It's actually even worse than that: p have a destructor (and said destructor is, of course, the whole reason std::unique_ptr exists) thus it's non-trivial for the purposes of call which, in turn, means, that destruction of the object pointed by p still happens in the caller, not callee and, maybe even more importantly, std::unique_ptr can not be passed in register.

Clang have attribute which solves that issue, but libstdc++ doesn't use it (and I'm not even sure if libc++ actually uses that attribute already).

That's why I know some companies which demand that std::unique_ptr shouldn't be used to pass arguments to function and return results from it. Instead you have to use owner<T> syntax and turn it into std::unique_ptr in your function and back when you call anything.

As I have said: “modern C++” is just a huge collection of footguns of different sizes and shapes thus it sounds really funny when it's proponent start talking about artificial puzzles the language imposes on top of each programming task.

At least these rules exist in some kind of central location and compiler actually verifies them! Much better than when their violation turns your program into pile of goo without any complains from the compiler.


to post comments

Ownership and lifetimes

Posted Jul 14, 2021 13:04 UTC (Wed) by mathstuf (subscriber, #69389) [Link] (1 responses)

> (I hope `p` just means p in your message and not some new footgun C++ gained while I wasn't looking)

I tend to just use "Plain text" here because HTML is a pain to write by hand and seem to wish that Markdown of some kind worked :) . That said, I cannot guarantee that it isn't a footgun you weren't aware of ;) .

> std::unique_ptr can not be passed in register.

True. There's discussion about breaking the ABI for this (and other things), but ABI breaking is a *huge* topic and you have the monorepo-using "we rebuild everything ourselves so what even is ABI stability" on one side and the "we have critical libraries we cannot recompile anymore because we lost the source 10 years ago" on the other. I have no idea if that discussion will go anywhere other than heat generation, but I hope something *useful* comes out of it at least even if it is just "let's design in some escape hatches for ourselves in the future".

Ownership and lifetimes

Posted Jul 17, 2021 16:06 UTC (Sat) by mrugiero (guest, #153040) [Link]

Then you use an older system I guess. If you were fine with no updating your software you're probably fine with that too.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds