News for hackers. Stuff that matters.
Re:My favorite Rust function by jcelerier on Saturday October 12, 2019 @07:03PM (#21236976)
The standard C++ way to free resources is the character '}'
Re:My favorite Rust function by wwright on Saturday October 12, 2019 @07:11PM (#21237005)
Rust has the exact same semantics there. Drop is useful when you need to explicitly notate that a value should end its life early.
Re:My favorite Rust function by klipt on Saturday October 12, 2019 @07:23PM (#21237054)
void drop(unique_ptr<T>&& x) {}

Would do the same in C++ for values held by unique_ptr: take ownership of the pointer and then free it.

Re:My favorite Rust function by SamReidHughes on Saturday October 12, 2019 @07:26PM (#21237064)
No, it would not free it. It takes a reference to the pointer and does nothing.
Re:My favorite Rust function by dkersten on Saturday October 12, 2019 @08:04PM (#21237242)
unique_ptr disposes of the object its holding when it goes out of scope unless passed to another unique_ptr or ownership is explicitly released. Presumably klipt would std::move the unique_ptr, hence the universal reference (which seems unnecessary, just pass it by value).

Am I missing something? Does it not work with std::move?

Re:My favorite Rust function by Rusky on Saturday October 12, 2019 @08:07PM (#21237264)
Yes, you're missing that the type `T&&` is a reference, not a value, and so does not run the object's destructor when it goes out of scope.
Re:My favorite Rust function by dkersten on Saturday October 12, 2019 @08:24PM (#21237336)
Sorry, I edited right after hitting reply to clarify that I'm assuming the intention was to std::move the unique_ptr in, since it was T&& and not T&. Would that still not work?
Re:My favorite Rust function by Rusky on Saturday October 12, 2019 @08:35PM (#21237381)
It would still not work. All std::move does is cast to T&&, enabling overload resolution to pick a function with T&& as its parameter type.
Re:My favorite Rust function by dkersten on Sunday October 13, 2019 @12:36AM (#21238385)
Thanks for explaining!
Re:My favorite Rust function by MauranKilom on Saturday October 12, 2019 @10:32PM (#21237919)
It would have to be

void drop(std::unique_ptr<T> x) {}

Or, you know, just call someX.reset(); or do someX = nullptr;. The drop call would be way noisier: drop(std::move(someX));

Re:My favorite Rust function by edflsafoiewq on Saturday October 12, 2019 @08:20PM (#21237319)
The equivalent to drop in C++ would be

    template <class T>
    void drop(T&) = delete; // force callers to move

    template <class T>
    void drop(T&& x) {
        T drop_me(std::move(x));
    }
edit: fixed.
Re:My favorite Rust function by Rusky on Saturday October 12, 2019 @08:53PM (#21237481)
This is not correct; moves must leave the value in a valid state, because its destructor will still run. The correct version is actually the same as Rust's:

    template <class T>
    void drop(T) {}
(Moving into a local in `drop(T&&)` also works.)
Re:My favorite Rust function by edflsafoiewq on Saturday October 12, 2019 @09:02PM (#21237515)
You're correct, sorry (I did have that at one point...). But the equivalent of Rust's pass-by-moving is to pass an r-value reference. Passing a const reference to drop is certainly an error and should be disallowed.
Re:My favorite Rust function by Filligree on Saturday October 12, 2019 @07:58PM (#21237217)
Re:My favorite Rust function by Rusky on Saturday October 12, 2019 @08:06PM (#21237253)
NLL does not change those semantics; drop still runs at the end of the scope. It counts as a "use" for the purposes of NLL and thus keeps values with destructors alive.
Re:My favorite Rust function by steveklabnik on Saturday October 12, 2019 @09:47PM (#21237727)
For some history, it was talked about changing this, but we couldn’t, due to back compatibility and it wasn’t clear it was actually a good idea. This was called “wary drop”.
Re:My favorite Rust function by likeliv on Sunday October 13, 2019 @03:03AM (#21238871)
Would be nice if types could annotate their Drop trait with #[early_drop] if the drop has no visible side effect, do rust could free the memory earlier.
Re:My favorite Rust function by steveklabnik on Sunday October 13, 2019 @12:38PM (#21241291)
“Early drop”, thanks autocorrect.
Re:My favorite Rust function by perspective1 on Saturday October 12, 2019 @07:17PM (#21237022)
Tell that to the enormous heap object you just forgot you made earlier.
Re:My favorite Rust function by klipt on Saturday October 12, 2019 @07:21PM (#21237041)
It would work with an object held by unique_ptr though, exactly like this Rust example.
Re:My favorite Rust function by saagarjha on Saturday October 12, 2019 @07:21PM (#21237040)
You’re not using std::unique_ptr to manage that?