activate RTTI in c++
Asked Answered
E

7

39

Can anybody tell me how to activate RTTI in c++ when working on unix. I heard that it can be disabled and enabled. on my unix environment,how could i check whether RTTI is enabled or disabled?

I am using the aCC compiler on HPUX.

Encephalomyelitis answered 14/4, 2010 at 5:42 Comment(0)
P
31

Are you using g++ or some other compiler?

In g++ RTTI is enabled by default IIRC, and you can disable it with -fno-rtti. To test whether it is active or not use dynamic_cast or typeid

UPDATE

I believe that HPUX's aCC/aC++ also has RTTI on by default, and I am unaware of a way to disable it. Check your man pages.

Pentothal answered 14/4, 2010 at 5:47 Comment(1)
I am using aCC (ansi c) compiler for compiling my C++ programs.Encephalomyelitis
V
24

gcc has it on by default. Check if typeid(foo).name() gives you something useful:

#include <iostream>
#include <typeinfo>

int main()
{
 std::cout << typeid(int).name() << std::endl;
 return 0;
}

Without RTTI you get something like:

foo.cpp:6: error: cannot use typeid with -fno-rtti
Vibrant answered 14/4, 2010 at 5:46 Comment(0)
B
8

According to the docs there is no option to turn it off. The only two bits of standard C++ that can be selectively disabled are "scope of variables in for loops" (-Wc,ansi_for_scope,off) and Argument-Dependent Lookup of names (-Wc,-koenig_lookup,off). There's no option similar to -Wc,-RTTI,off

Brisson answered 14/4, 2010 at 9:8 Comment(0)
B
6

All modern C++ compilers I know (GCC, Intel, MSVC, SunStudio, aCC) have RTTI enabled by default, so unless you have any suspects that it may be disabled for some reason you may safely assume that RTTI in on.

Bibcock answered 14/4, 2010 at 6:36 Comment(0)
C
3

RTTI will be enabled or disabled when compiling your program via compiler options - it's not something enabled or disabled in the Unix environment globally. The easiest way to see if it's enabled by default for your compiler is to just try compiling some code using RTTI.

Options to enable/disable RTTI will be compiler specific - what compiler are you using?

RTTI support is on by default in GCC, the option -fno-rtti turns off support (in case you're using GCC and maybe someone's turned off RTTI in a makefile or something).

Counterespionage answered 14/4, 2010 at 5:47 Comment(0)
P
2

Enabling and disabling RTTI must be a compiler specific setting. In order for the dynamic_cast<> operation, the typeid operator or exceptions to work in C++, RTTI must be enabled. If you can get the following code compiled, then you already have RTTI enabled (which most compilers including g++ do automatically):

#include <iostream>
#include <typeinfo>

class A {
    public:
        virtual ~A () { }
};

class B : public A { };

void rtti_test(A& a) {
    try {
        B& b = dynamic_cast<B&>(a);
    } catch (std::bad_cast) {
        std::cout << "Invalid cast.\n";
    }

    std::cout << "rtti is enabled in this compiler.\n";
}

int main() {
    A *a1 = new B;
    rtti_test(*a1);  //valid cast
    A *a2 = new A;
    rtti_test(*a2);  //invalid cast

    return 0;
}
Postmeridian answered 14/4, 2010 at 6:8 Comment(2)
what would happen if ii compile with -fno-rtti? Would the compiler complain of using virtuals?Nonfulfillment
Just for somebody who curious about relationship between exceptions and RTTI. Here is discussion of the topic: #10320572 Summarizing, exceptions do not need RTTI to be enabled. As it does not use it's special features like dynamic_cast and typeid. But seems like it still uses some run-time type information for matching exceptions.Arlynearlynne
C
1

In g++ you can test the __GXX_RTTI macro to see if RTTI is on in your code. As others have pointed out -no-rtti turns of RTTI in g++. I would bet both these things work in clang as well.

#ifdef __GXX_RTTI
  w = dynamic_cast<FooBah*>(FooFactory(TYPE, arg));
  if (w != NULL)
  {
    if (w->thing == OK)
      FeastOnOrangUtansAndFruitBatsAndBreakfastCereals();
  }
#endif

In newer C++ we will have access to feature testing macros __cpp_rtti and many others that will make tese things easier.

Communistic answered 8/10, 2014 at 17:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.