Is there a C ++ equivalent for Rust `std :: mem :: drop` in the standard library?

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));
// f is closed now, and everything is flushed to disk
+6
source share
1

++ . :

SomeType var = ...;
//do stuff with `var`.
{auto _ = std::move(var);}
//The contents of `var` have been destroyed.

, ++ Rust var. , ++ , , , .

, , . , lock_guard, , . , - .

+6

All Articles