Re: Will we ever be able to throw from a destructor?
DeMarcus <use_my_alias_here@hotmail.com> writes:
On 06/05/2012 09:51 PM, Michael Kilburn wrote:
Anyway, idea is to have smth like this:
struct Foo
{
~Foo(); // called when we leave the scope normally (and
can throw)
~~Foo() throw(); // called by stack unwinding
};
If ~~Foo() is not defined -- it points (aliases) to ~Foo(), which
becomes nothrow. This is relatively simple mechanism, would be really
nice to have it in C++.
Could you simulate that with std::uncaught_exception? Like so:
Foo::DtorFooUnwind() noexcept
{
// Do what you need to do when the stack is unwinding.
}
Foo::~Foo()
{
if( std::uncaught_exception() )
{
DtorFooUnwind();
return;
}
// Else, commit to DB or other close-down functionality.
}
Your Foo also has to remember whether it had been constructed during
stack unwinding.
This has already been discussed some years ago, either here or over in
comp.lang.c++. I don't have a message reference, but I found the
following code lying around on my harddrive (from August 2007):
--- 8< ---
#include <iostream>
#include <exception>
#include <cassert>
struct Unwind
{
bool constructedDuringException;
Unwind() : constructedDuringException(std::uncaught_exception()) {}
bool stack_unwinding() const
{
return !constructedDuringException && std::uncaught_exception();
}
};
struct Lonely : private Unwind {
~Lonely()
{
std::cout << "~Lonely(): uncaught=" << std::uncaught_exception()
<< ", unwinding=" << stack_unwinding() << std::endl;
}
};
struct Inner : private Unwind {
~Inner()
{
std::cout << "~Inner(): uncaught=" << std::uncaught_exception()
<< ", unwinding=" << stack_unwinding() << std::endl;
assert( stack_unwinding() == false );
} // contrary to uncaught_exception
};
struct Outer : private Unwind {
~Outer()
{
std::cout << "~Outer(): uncaught=" << std::uncaught_exception()
<< ", unwinding=" << stack_unwinding() << std::endl;
assert( stack_unwinding() == true ); // like uncaught_exception
Inner in;
}
};
void fun()
{
Outer out;
throw std::exception();
}
int main()
{
Lonely l;
try { fun(); } catch (...) {}
}
--- 8< ---
Regards,
Stefan
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]