在 C++ 中激活 RTTI
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2635123/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
activate RTTI in c++
提问by Vijay
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?
谁能告诉我在 unix 上工作时如何在 c++ 中激活 RTTI。听说可以禁用和启用。在我的 unix 环境中,我如何检查 RTTI 是启用还是禁用?
I am using the aCC
compiler on HPUX.
我aCC
在 HPUX 上使用编译器。
采纳答案by vladr
Are you using g++
or some other compiler?
您使用的是g++
其他编译器吗?
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
在g++
RTTI 中默认启用 IIRC,您可以使用-fno-rtti
. 要测试它是否处于活动状态,请使用dynamic_cast
或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.
我相信 HPUX 的aCC
/aC++
默认情况下也启用了 RTTI,而且我不知道有什么方法可以禁用它。检查您的man
网页。
回答by Eddy Pronk
gcc has it on by default. Check if typeid(foo).name() gives you something useful.
gcc 默认开启。检查 typeid(foo).name() 是否给你一些有用的东西。
#include <iostream>
#include <typeinfo>
int main()
{
std::cout << typeid(int).name() << std::endl;
return 0;
}
Without RTTI you get something like:
没有 RTTI,你会得到类似的东西:
foo.cpp:6: error: cannot use typeid with -fno-rtti
回答by MSalters
According to the docsthere 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
根据文档,没有关闭它的选项。可以有选择地禁用的标准 C++ 中仅有的两个位是“for 循环中的变量范围”( -Wc,ansi_for_scope,off
) 和名称的参数相关查找 ( -Wc,-koenig_lookup,off
)。没有类似的选项-Wc,-RTTI,off
回答by Artyom
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.
我知道的所有现代 C++ 编译器(GCC、Intel、MSVC、SunStudio、aCC)都默认启用了 RTTI,因此除非您怀疑它可能由于某种原因被禁用,否则您可以放心地假设 RTTI 已启用。
回答by Michael Burr
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.
通过编译器选项编译程序时将启用或禁用 RTTI - 在 Unix 环境中全局启用或禁用它。查看编译器是否默认启用它的最简单方法是尝试使用 RTTI 编译一些代码。
Options to enable/disable RTTI will be compiler specific - what compiler are you using?
启用/禁用 RTTI 的选项将特定于编译器 - 您使用的是什么编译器?
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).
GCC 中默认启用 RTTI 支持,该选项-fno-rtti
关闭支持(如果您使用 GCC 并且可能有人在 makefile 或其他内容中关闭了 RTTI)。
回答by Vijay Mathew
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):
启用和禁用 RTTI 必须是编译器特定的设置。为了使dynamic_cast<>
操作、typeid
运算符或异常在 C++ 中工作,必须启用 RTTI。如果您可以编译以下代码,那么您已经启用了 RTTI(包括 g++ 在内的大多数编译器都会自动启用):
#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;
}
回答by emsr
In g++ you can test the __GXX_RTTI
macroto 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.
在G ++可以测试的__GXX_RTTI
宏观,看是否RTTI是在你的代码。正如其他人指出的那样 -no-rtti 在 g++ 中转为 RTTI。我敢打赌,这些东西也适用于 clang。
#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.