The function std::mem::drop in Rust moves its argument and then destroys it by going out of scope. My attempt at writing a similar function in C++ looks like this:
template <typename T,
typename = std::enable_if_t<std::is_rvalue_reference<T &&>::value>>
void drop(T &&x) {
T(std::move(x));
}
Does such a function already exist in the standard library?
Edit: The function can be used to invoke the destructor of an object before going out of scope. Consider a class that closes a file handle as soon as it is destroyed, but not earlier. For the sake of the argument, suppose ofstream did not have a close method. You can write:
ofstream f("out");
f << "first\n";
drop(move(f));
// f is closed now, and everything is flushed to disk