destroy
PrototypeDestroy is an overloaded name; there are actually two destroy functions. template <class T> void destroy(T* pointer); template <class ForwardIterator> void destroy(ForwardIterator first, ForwardIterator last); DescriptionIn C++, the operatordelete destroys an object by calling its destructor, and then deallocates the memory where that object was stored. Occasionally, however, it is useful to separate those two operations. [1] Destroy calls an object's destructor without deallocating the memory where the object was stored.
The first version of
The second version of DefinitionDefined in the standard header memory, and in the nonstandard backward-compatibility header algo.h. Thedestroy algorithms are no longer part of the C++ standard; they were present in early drafts, and they are retained in this implementation for backward compatibility. Requirements on typesFor the first version ofdestroy:
destroy:
PreconditionsFor the first version ofdestroy:
destroy:
ComplexityThe run-time complexity of the second version is linear: it calls the destructor exactlylast - first times. Exampleclass Int { public: Int(int x) : val(x) {} int get() { return val; } private: int val; }; int main() { Int A[] = { Int(1), Int(2), Int(3), Int(4) }; destroy(A, A + 4); construct(A, Int(10)); construct(A + 1, Int(11)); construct(A + 2, Int(12)); construct(A + 3, Int(13)); } Notes[1] In particular,destroy, along with other low-level memory allocation primitives, is used to implement container classes. See alsoAllocators,construct, uninitialized_copy, uninitialized_fill, uninitialized_fill_n, raw_storage_iterator | |||||||

