Search code examples
c++gccrtti

GCC C++ override -frtti for single class


Is there a way to avoid generating of typeinfo for whole translation unit or some class when it is compiled with -frtti ?

Maybe there is a magic #pragma or __attribute__ that can override command line option?

Thanks in advance.


Solution

  • It seems for me, that there is no possible magic. Moreover, it seems for me that any kind of such magic would be disruptive.

    The only magical GCC pragma that allow user to fine-tune compilation options from inside code is pragma GCC optimize.

    This pragma works function-wise, because optimizer itself works function-wise and you can easily see, that it has no effect on RTTI generation for types:

    #include <iostream>
    #include <typeinfo>
    
    struct X
    {
      virtual int foo() {return 0;}
    };
    
    #pragma GCC optimize ("no-rtti")
    
    struct Y
    {
      virtual int foo() {return 0;}
    };
    
    #pragma GCC reset_options
    
    int
    main ()
    {
      std::cout << "X: " << sizeof (X) << " " << typeid(X).name() << std::endl;
      // next line should *NOT* build/link if the pragma was taken into account
      std::cout << "Y: " << sizeof (Y) << " " << typeid(Y).name() << std::endl;
      return 0;
    }
    

    In GCC 5.2, output is:

    X: 8 1X
    Y: 8 1Y