Re: dynamic_cast & Exceptions
On Mar 4, 4:43 pm, Al <t...@haik.us> wrote:
Say we have:
class A {};
class B : public A {};
A* foo();
int main() {
dynamic_cast<B&>(*foo());
return 0;
}
Modulo any typos, this should work, assuming foo() returns what is
really a B*. Next, if foo() returns a different derived class of A*,
dynamic_cast is supposed to throw (since it's a reference).
And since you don't catch the exception, the program aborts.
However, what happens if:
i) foo() calls assert();
If the assertion fails, the program aborts. Immediately.
ii) foo() throws an exception.
The exception is thrown.
Does the dynamic_cast exception override the user exception/assert?
If foo() throws an exception or aborts, the dynamic_cast never
gets executed. The results of foo() are an operand of the
dynamic_cast, and so must be available before dynamic_cast can
be executed.
Is it UB, or implementation-specific?
Neither. It's all very specified.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]