std :: type_info :: name()的实际目的是什么? [英] What is the actual purpose of std::type_info::name()?

查看:336
本文介绍了std :: type_info :: name()的实际目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天,我的一个同事来了,问我标题中提到的问题。

他目前正试图减少代码库的二进制文件占用空间,也用于小目标(如Cortex M3和类似)。显然,他们决定编译与RTTI开启(GCC实际上),以支持正确的异常处理。



好吧,他的主要抱怨为什么 std :: type_info :: name() 实际上需要支持RTTI,并且要求,如果我知道一种方法,只是压缩生成的字符串字面量需要支持这个,或至少缩短它们。


std :: type_info :: name



const char * name()const;
返回一个实现定义的以null结束的字符串,包含类型的名称。不提供保证,特别是返回的字符串对于多个类型可以是相同的,并且在同一程序的调用之间改变。


A , - 但是编译器特定 - ,实现eg dynamic_cast<> 运算符不会使用此信息,而是类似于哈希标记类型确定(类似于 catch code>具有异常处理的块)。

我认为后者由

的当前标准定义清楚地表示


  1. std :: type_info :: hash_code

  2. std :: type_index

我不得不同意,我也使用 std :: type_info :: name(),除了调试(日志记录)的目的之外,并不真正看到一点。我不是100%肯定异常处理将工作,没有RTTI与当前版本的GCC(我认为他们使用4.9.1),所以我犹豫,建议只是关闭RTTI。

也是在它们的代码库中使用 dynamic_casts<> 的情况,但是对于这些,我只是建议不要使用它,赞成 static_cast (它们实际上没有类似于插件的东西,或者除了断言之外还需要运行时类型检测)。






问题:




  • 是否有真实的生活,生产代码级用例 std ::



子问题(更具体): h3>


  • 有没有人有一个想法,如何克服(解决)这些无用的字符串字面量的生成)


  • 是否需要RTTI支持GCC的异常处理?

    通过 @ Sehe的回答,我已经接受了。剩下的另一个子问题是为代码中使用的任何异常生成 std :: type_info 实例。我们确定,这些文字在任何地方都不会使用)







相关位: 删除未使用的运行时函数

隔离此位:





answer is yes:


-fno-rtti



禁止使用虚拟函数生成每个类的信息,以供C ++运行时类型识别功能使用( dynamic_cast typeid )。如果不使用语言的这些部分,可以使用此标志来节省一些空间。 请注意,异常处理使用相同的信息,但会根据需要生成 dynamic_cast 运算符仍然可以用于不需要运行时类型信息的转换,即转换为 void * 或非明确的基类。



Today a colleague of mine came and asked me the question as mentioned in the title.
He's currently trying to reduce the binaries footprint of a codebase, that is also used on small targets (like Cortex M3 and alike). Apparently they have decided to compile with RTTI switched on (GCC actually), to support proper exception handling.

Well, his major complaint was why std::type_info::name() is actually needed at all for support of RTTI, and asked, if I know a way to just suppress generation of the string literals needed to support this, or at least to shorten them.

std::type_info::name

const char* name() const; Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given, in particular, the returned string can be identical for several types and change between invocations of the same program.

A ,- however compiler specific -, implementation of e.g. the dynamic_cast<> operator would not use this information, but rather something like a hash-tag for type determination (similar for catch() blocks with exception handling).
I think the latter is clearly expressed by the current standard definitions for

  1. std::type_info::hash_code
  2. std::type_index

I had to agree, that I also don't really see a point of using std::type_info::name(), other than for debugging (logging) purposes. I wasn't a 100% sure that exception handling will work just without RTTI with current versions of GCC (I think they're using 4.9.1), so I hesitated to recommend simply switching off RTTI.
Also it's the case that dynamic_casts<> are used in their code base, but for these, I just recommended not to use it, in favor of static_cast (they don't really have something like plugins, or need for runtime type detection other than assertions).


Question:

  • Are there real life, production code level use cases for std::type_info::name() other than logging?

Sub-Questions (more concrete):

  • Does anyone have an idea, how to overcome (work around) the generation of these useless string literals (under assumption they'll never be used)?

  • Is RTTI really (still) needed to support exception handling with GCC?
    (This part is well solved now by @Sehe's answer, and I have accepted it. The other sub-question still remains for the left over generated std::type_info instances for any exceptions used in the code. We're pretty sure, that these literals are never used anywhere)


Bit of related: Strip unused runtime functions which bloat executable (GCC)

解决方案

Isolating this bit:

The answer is yes:

-fno-rtti

Disable generation of information about every class with virtual functions for use by the C++ runtime type identification features (dynamic_cast and typeid). 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. The dynamic_cast operator can still be used for casts that do not require runtime type information, i.e. casts to void * or to unambiguous base classes.

这篇关于std :: type_info :: name()的实际目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆