Algorithms that affect object lifetime
The <memory>
header provides an obscure family of algorithms with names such as std::uninitialized_copy
, std::uninitialized_default_construct
, and std::destroy
(for the full list, consult an online reference such as cppreference.com). Consider the following algorithm that uses explicit destructor calls to destroy the elements of a range:
template<class T> void destroy_at(T *p) { p->~T(); } template<class FwdIt> void destroy(FwdIt first, FwdIt last) { for ( ; first != last; ++first) { std::destroy_at(std::addressof(*first)); } }
Notice that std::addressof(x)
is a convenient little helper function that returns the address of its parameter; it's exactly the same thing as &x
except in the rare case that x
is of some class type that sadistically overloads its own operator&
.
And consider this algorithm that uses explicit placement-new syntax to "copy-construct into" the elements...