本文介绍了我怎么知道如果std :: type_index是唯一的我的编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标准是否规定对 std :: type_index(typeid(obj))的调用对该类型是唯一的?我找不到这方面的信息。从 type_info :: name()我得到这个:

Does the standard prescribe that an invocation of std::type_index(typeid(obj)) will be unique to that type? I couldn't find information on this. From type_info::name() I got this:

(来源:)

这让我相信,可能不正确的名称/类型不是必须是唯一的。但是的专页使用假设,这些类型在其中是唯一的。

Which leads me to believe that maybe the mangled names / typeids are not necessarily unique. Yet the page for std::type_index specifically uses the assumption that these types are unique in its "usage example".

那么如何知道我的编译器的 typeid 是否是唯一的, ?此外,有什么方法可以获得某种类型的标识符(无论是字符串还是其他),我们可以知道是该类型所独有的吗?

So how can I know if the typeid for my compiler is unique, and what the likelihood for a collision is? Further, is there any way of getting some sort of identifier (whether a string or otherwise) that we can know is unique to the type?

Demangling不是一个选项,因为它对于非常大的类型来说太慢了,但我的猜测是,如果实现提供了一个名称解构的工具,

Demangling is not an option because it is too slow for very large types, but my guess is that if the implementation provides a facility to demangle a name, then the mangled name should be unique for that type in that implementation anyway, correct?

推荐答案

<$ c $>在 type_info 上的c> name 函数不是有用的。一个实现可能返回的一切,并符合。在实践中,并不总是,它只能用于调试。

The name function on a type_info is not guarenteed to be useful. An implementation could probably return "" for everything and be compliant. In practice it is not always "", and it can be used for debugging, but only that.

type_info == 之前 hash_code 不依赖于名称

type_index 是指向 type_info 的指针的包装,它使用info的方法来生成Regular类型(可以复制,存储等) 。对于不同的类型它是不同的。

type_index is a wrapper around a pointer to a type_info that uses the info's methods to produce a Regular type (can be copied, stored, etc). It is distinct for distinct types.

这篇关于我怎么知道如果std :: type_index是唯一的我的编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 14:08