A function std::mem::dropin Rust moves its argument and then destroys it, going out of scope. My attempt to write a similar function in C ++ is as follows:
template <typename T,
typename = std::enable_if_t<std::is_rvalue_reference<T &&>::value>>
void drop(T &&x) {
T(std::move(x));
}
Does this feature already exist in the standard library?
Edit: This function can be used to call the object's destructor before exiting the scope. Consider a class that closes a file descriptor as soon as it is destroyed, but not earlier. For an argument, suppose it ofstreamdoes not have a method close. You can write:
ofstream f("out");
f << "first\n";
drop(move(f));
source
share