Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable RTTI for some classes

Tags:

c++

g++

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.

like image 361
Puppy Avatar asked May 28 '14 13:05

Puppy


People also ask

How do I enable RTTI in C++?

Run-Time Type Discovery (Reflection) Note: The compiler option Project > Options > C++ Compiler > Enable RTTI refers to standard C++ RTTI.

What is FNO Rtti?

-fno-rtti. Disable generation of information about every class with virtual functions for use by the C++ runtime type identification features (` dynamic_cast ' and ` typeid '). If you don't use those parts of the language, you can save some space by using this flag.

What is Rtti C++?

Run-time type information (RTTI) is a mechanism that allows the type of an object to be determined during program execution. RTTI was added to the C++ language because many vendors of class libraries were implementing this functionality themselves.


1 Answers

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.
like image 199
n. 1.8e9-where's-my-share m. Avatar answered Sep 27 '22 20:09

n. 1.8e9-where's-my-share m.