3 ms·
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)
by edflsafoiewq 7y ago
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.
- Rusky 7y agoThis 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.)
- edflsafoiewq 7y agoYou'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.