相同的typeid名称,但不是std :: is_same [英] Same typeid name but not std::is_same

查看:94
本文介绍了相同的typeid名称,但不是std :: is_same的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C ++(gcc 4.8.3)我有2种类型( T1 T2 typeid(T1).name() typeid(T2).name()相同的属性b $ b std :: is_same< T1,T2> :: value false

Using C++ (gcc 4.8.3) I have 2 types (T1 and T2) which have the strange property that typeid(T1).name() and typeid(T2).name() are the same but std::is_same<T1, T2>::value is false.

怎么可能?我该如何进一步调查以找出原因?

How can that be? How can I investigate further to tell what the reason might be ?

推荐答案

忽略多态, typeid() 给您一个表示表达式静态类型的对象。但是,对于表达式类型,某些元素将被忽略。来自[expr]:

Ignoring polymorphism, typeid() gives you an object representing the static type of the expression. But there are certain elements that are ignored when it comes to expression types. From [expr]:


如果表达式最初的类型为对 T (8.3.2,8.5.3),在
进行任何进一步分析之前,将类型调整为 T 。 [...]如果一个prvalue最初的类型为 cv T ,其中 T cv 的不合格非类,非数组类型,
的类型将表达式调整为 T 之前的

If an expression initially has the type "reference to T" (8.3.2, 8.5.3), the type is adjusted to T prior to any further analysis. [...] If a prvalue initially has the type "cv T", where T is a cv-unqualified non-class, non-array type, the type of the expression is adjusted to T prior to any further analysis.

因此,任何仅在顶级 cv -资格或参考将产生相同的typeid。例如,类型 int const int int& volatile const int& ,等等都给您相同的 typeid()

As a result, any types which differ only in top-level cv-qualification or reference will yield the same typeid. For instance, the types int, const int, int& volatile const int&&, etc all give you the same typeid().

基本上,您最初的思考过程是:

Basically, your initial thought process was:

typeid(T) == typeid(U) <==> std::is_same<T, U>

但正确的对等是:

typeid(T) == typeid(U) <==> std::is_same<expr_type<T>, expr_type<U>>

其中:

template <class T>
using expr_type = std::remove_cv_t<std::remove_reference_t<T>>;

这篇关于相同的typeid名称,但不是std :: is_same的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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