7

I've got a C++ translation unit and I need to disable RTTI for two classes in it, but nothing else. Is there something like #pragma rtti(off) or something that I can use?

I need to disable RTTI for that class only. I do not throw or catch or dynamic_cast or anything this class, so I simply don't need the RTTI for it. The implementation of it's methods certainly need to be compiled with RTTI on, as they can indeed throw exceptions, it's just the generation of this one typeinfo object that I need to suppress.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • That would depend on your compiler. But probably not. X-Why problem? – aschepler May 28 '14 at 13:30
  • Can't you move those two classes to a separate file and turn off RTTI for just that file? – Praetorian May 28 '14 at 13:32
  • @Praetorian: Well, the classes are supposed to be private to that particular file, and their implementation throws exceptions in some cases. – Puppy May 28 '14 at 13:36
  • @aschepler: The question is conveniently tagged "g++". – Puppy May 28 '14 at 13:36
  • 6
    This really really stin^H^H^Hmells like an XY problem! What are you achieving with disabling rtti? – PlasmaHH May 28 '14 at 15:11
  • Disabling RTTI will not make them stop throwing those exceptions. – n. m. could be an AI May 28 '14 at 16:52
  • If you disable RTTI for a class that throws, it is unlikely to have any effect. If you disable exceptions for code that throws, it will not compile. If you disable exceptions for code that passes exceptions through, you will cause it to break silently and irrecoverably at runtime. If you disable RTTI for a class that is thrown, throwing and/or catching it will cause a fatal error at runtime. What of the above do you want? – n. m. could be an AI May 28 '14 at 17:02
  • 1
    Implementing exceptions does not require the RTTI object for random other arguments to the function which throws, including `this`. FTR, I want to do this because it inherits from another class in another codebase which is compiled without RTTI, which I cannot change. I need to use RTTI in my codebase but I certainly don't need RTTI for this specific derived class, only the implementations of its methods. – Puppy May 28 '14 at 17:09
  • That's a legit reason, you should have said that in the question. – n. m. could be an AI May 28 '14 at 19:02
  • 1
    @n.m.: I didn't realize that the reason why I wanted to do it affected the means available (or not, as the case may be) to do it. – Puppy May 28 '14 at 22:25

2 Answers2

3

To disable RTTI in g++ for a particular class and nothing else (tested on a limited test case, exercise caution):

  1. Move the class definition to a separate header file.
  2. Add a new virtual function virtual void nortti(); to your class. Make it the very first virtual function.
  3. Put its implementation to a separate source file. Compile this file with fno-rtti.
  4. Compile the rest of the class implementation normally.
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
2

If you're using Microsoft's compiler you can use __declspec(novtable) to turn off the vtable for a specific class. This has the side effect of causing RTTI to fail for that class. It also has the side effect of leaving you unable to use virtual functions if it's a concrete class, which is a pretty serious drawback - use with care. It was designed for use with interfaces where the base class would never be instantiated.

Apparently there's no similar ability in g++: Is there a g++ equivalent to Visual Studio's __declspec(novtable)?

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622