Destructors cannot throw exceptions (so stack expansion can end during exception handling) and must release any resources allocated to the object (therefore, there is no leakage of resources). The construction of an object that contains several other objects (or several resources are allocated) can write pointers to them in the STL container. Therefore, the destructor would use the following methods associated with the iterator:
begin() , end() for the containeroperator++ for a valid iteratoroperator* or operator-> for a valid iterator
But to ensure that the destructor does not throw exceptions and free resources, you will need to rely on methods that never throw exceptions.
Can you rely on these methods without ever throwing exceptions? It is hard to imagine a practical implementation that will throw exceptions, since under the hood the STL iterator is essentially a pointer. But does standard C ++ have a request that these methods never throw an exception? I did not find a clear statement in the C ++ standard.
Change An interesting case for C ++ 03 if you want to have a container of pointers to resources . There are good reasons for this; for example, if you have polymorphic resources. Since BjΓΆrn Pollex indicates in its answer that if you use a resource container (e.g. std::list< Resource > ) and not a container of resource pointers, the container destructor will take care of destroying (releasing) the Resource objects for you.
c ++ exception stl
Raedwald Oct 26 '11 at 12:18 2011-10-26 12:18
source share