17

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?

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
qdii
  • 12,505
  • 10
  • 59
  • 116
  • AFAIR MSVC 5 or 6 had rtti off and exceptions on by default. Might be wrong, it was long time ago. – Agent_L Apr 25 '12 at 17:17
  • This article on the [orthogonality of RTTI and exceptions](http://monoinfinito.wordpress.com/2013/07/25/c-exceptions-under-the-hood-appendix-iii-rtti-and-exceptions-orthogonality/) 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. – Chuim Sep 17 '14 at 12:51

3 Answers3

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.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 22
    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. – edA-qa mort-ora-y Apr 25 '12 at 20:42
8

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

sjoerd
  • 81
  • 1
  • 1
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.

Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 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. – Dan Albert Aug 11 '17 at 17:54