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):
and a possible use:
As you can see the AutoPtr::reset() deletes the stored pointer and then is not able to nullify it due the throw, as soon as the "a" instance goes out of scope due the stack unwinding then ~AutoPtr deletes again thePointer. A possible implementation of reset can be the following:
but unfortunately it not saves you! Indeed in c++11 specification you can "find" the following:
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
does generate a crash if compiled in c++11 mode with gcc (4.8 and 4.9) and clang (3.5) while with intel icc 14.01 doens't call the std::unexpected either the std::terminate (time to fill an icc bug?)
3 comments:
there's a missing 'k' ;-)
good to kno indeed!
Given how the language is defined, you are absolutely right. Yet I wonder if exception chaining wouldn't be a better approach than termination. It has to be said that it would make it very difficult to avoid memory leaks in the presence of exceptions.
The purpose of constructor is not hard to understand. but for me, I am still unable to understand the usage of destructor.
C++ in Urdu
Post a Comment