If you still are not convinced by those arguments then I hope you will buy at least the following. Let's look at a possible implementation of an unique ptr (apart the -> and * operators):
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <class T> | |
class AutoPtr { | |
public: | |
AutoPtr(T* aPointer) | |
: thePointer(aPointer) | |
{} | |
~AutoPtr() { | |
delete thePointer; | |
} | |
void reset() { | |
delete thePointer; | |
thePointer = nullptr; | |
} | |
private: | |
T* thePointer; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int main() { | |
AutoPtr<Bomb> a(new Bomb()); | |
a.reset(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void reset() { | |
T* tmp = thePointer; | |
thePointer = nullptr; | |
delete tmp; | |
} |
12.4.3: A declaration of a destructor that does not have an exception-specification is implicitly considered to have the same exception-specification as an implicit declaration (15.4).and again:
Whenever an exception is thrown and the search for a handler (15.3) encounters the outermost block of a function with an exception-specification that does not allow the exception, then, — if the exception-specification is a dynamic-exception-specification, the function std::unexpected() is called (15.5.2), — otherwise, the function std::terminate() is called (15.5.1).that means that throwing an exception from a DTOR terminates your program and it doesn't matter if a stack unwinding is going on or not.
This simple example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdexcept> | |
class Bomb { | |
public: | |
~Bomb() { throw std::runtime_error("BOOM"); } | |
}; | |
int main() | |
try { | |
Bomb b; | |
} | |
catch(...) {} |