activate RTTI in c++

天涯浪子 提交于 2019-11-27 03:03:18

问题


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.


回答1:


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.




回答2:


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



回答3:


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




回答4:


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.




回答5:


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).




回答6:


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;
}



回答7:


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.



来源:https://stackoverflow.com/questions/2635123/activate-rtti-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!