什么时候编译没有 RTTI 的 C++ 会导致问题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4486609/
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
When can compiling c++ without RTTI cause problems?
提问by McPherrinM
I'm using gcc's -fno-rtti
flag to compile my C++ without runtime type information.
我正在使用 gcc 的-fno-rtti
标志来编译我的 C++,而没有运行时类型信息。
Assuming I'm not using dynamic_cast<>
or typeid()
, is there anything that could lead me to later problems?
假设我没有使用dynamic_cast<>
or typeid()
,有什么可能会导致我以后出现问题吗?
采纳答案by Bowie Owens
Since your question is specific to GCC you should consult carefully the documentation for the version you are using. The documentation for GCC 4.5.2 says the following. Which from my reading would indicate that if you avoid dynamic_cast and typeid, you should be ok. That said, I have no personal experience with -fno-rtti. Perhaps you might like to elaborate on why you are using -fno-rtti.
由于您的问题特定于 GCC,您应该仔细查阅您所使用版本的文档。GCC 4.5.2 的文档说明如下。从我的阅读中可以看出,如果你避免使用 dynamic_cast 和 typeid,你应该没问题。也就是说,我对 -fno-rtti 没有个人经验。也许您可能想详细说明您使用 -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
andtypeid
). If you don't use those parts of the language, you can save some space by using this flag. Note that exception handling uses the same information, but it will generate it as needed. Thedynamic_cast
operator can still be used for casts that do not require runtime type information, i.e. casts tovoid *
or to unambiguous base classes.
-fno-rtti
禁止生成有关每个类的信息,这些类具有虚拟函数以供 C++ 运行时类型识别功能(dynamic_cast
和typeid
)使用。如果您不使用语言的这些部分,则可以通过使用此标志来节省一些空间。请注意,异常处理使用相同的信息,但会根据需要生成它。该dynamic_cast
运算符仍可用于不需要运行时类型信息的强制转换,即强制转换到void *
或到明确的基类。
There is discussion about the relationship between virtual functions and RTTI available at No RTTI but still virtual methods. The short version is that virtual functions should be fine without RTTI.
在No RTTI but still virtual methods 中讨论了虚函数和 RTTI 之间的关系。简短的版本是没有 RTTI 的虚函数应该没问题。
回答by Steve
We have used gcc without rtti for 5 years with no specific problems (not using dynamic_cast or typeid)
我们在没有 rtti 的情况下使用 gcc 5 年,没有出现特定问题(不使用 dynamic_cast 或 typeid)