Disable RTTI for some classes

c++, g++

Solution

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

- Move the class definition to a separate header file.

- Add a new virtual function `virtual void nortti();` to your class. Make it the very first virtual function.

- Put its implementation to a separate source file. Compile this file with `fno-rtti`.

- Compile the rest of the class implementation normally.

Problem

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.

Original source

Related problems