Is there a relation between RTTI and exceptions?
Asked Answered
J

3

19

I remember coding on platforms that had both RTTI and exceptions disabled, and on others that had them both enabled. However, I cannot remember coding on a platform that would enable one and disable the other one.

Is there any kind of dependency between the two concepts? Said differently, do exceptions need RTTI to function? Or the contrary?

Jampack answered 25/4, 2012 at 16:41 Comment(2)
AFAIR MSVC 5 or 6 had rtti off and exceptions on by default. Might be wrong, it was long time ago.Villareal
This article on the orthogonality of RTTI and exceptions adds to this topic. It shows that even though the exposed functionality is independent, exceptions in gcc actually do use their own version of "RTTI" to work.Kirtle
E
13

No, Exceptions do not need RTTI functionality neither vice versa both are separate features.

Some of the implementations might allow you to disable exceptions(-fnoexceptions in gcc) but I don't know of any implementation which needs RTTI for exceptions or vice versa.

Emir answered 25/4, 2012 at 16:43 Comment(1)
Perhaps exceptions don't need the explicit C++ RTTI features like type_info and dynamic_cast, but surely they need some kind of RTTI. The thrown exception has to be able to compare its type to the catch handlers on the exception stack, which involves some kind of RTTI.Oppilate
F
9

I was just reading this C++ proposal "Zero-overhead deterministic exceptions: Throwing values" (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0709r0.pdf?), in which I read:

"C++ allows there to be multiple active exception objects of arbitrary types, which must have unique addresses and cannot be folded; and it requires using RTTI to match handlers at run time, which has statically unpredictable cost on all major implementations and can depend on what else is linked into the whole program."

and elsewhere it is stated that:

"4) Today’s dynamic exceptions require using some form of RTTI to match handlers."

Thus, it appears there is a relation between exceptions and RTTI

Fraise answered 14/3, 2019 at 11:27 Comment(0)
M
1

They are not dependent on each other but they are both heavy features so if there is a platform that has bad performance they will probably both be cut together.

Modality answered 25/4, 2012 at 16:49 Comment(1)
This is a pretty outdated argument. Exceptions have zero runtime cost until they are thrown (which should only happen in exceptional cases). RTTI is similarly only a code size increase. If code size is a problem they both add size, but they do not hinder performance simply by being enabled.Guess

© 2022 - 2024 — McMap. All rights reserved.