[英]C++ type qualifiers and equality
Are int&
and int
the same type? int&
和int
是同一类型吗? if I use is_same<int,int&>::value
i get false
but typeid(int).name() == typeid(int&).name()
are the same? 如果我使用
is_same<int,int&>::value
我会得到false
但typeid(int).name() == typeid(int&).name()
相同吗?
secondly the same question for int
and const int
? 其次,对于
int
和const int
同样的问题吗?
Thirdly int
and int*
? 第三
int
和int*
吗?
I can understand if int
and int*
are not as one actually stores the address of another object and works differently but I would have thought int&
and int
are as one is just an alias for another. 我可以理解
int
和int*
不是因为一个实际上存储了另一个对象的地址并以不同的方式工作,但我会认为int&
和int
只是另一个的别名。
Keen to get some good commentary on this. 热衷于对此发表一些好的评论。
From Paragraph 5.2.7/4 of the C++11 Standard: 根据C ++ 11标准的5.2.7 / 4段:
When typeid is applied to a type-id, the result refers to a std::type_info object representing the type of the type-id.
当将typeid应用于type-id时,结果将引用代表该type-id类型的std :: type_info对象。 If the type of the type-id is a reference to a possibly cv-qualified type, the result of the typeid expression refers to a std::type_info object representing the cv-unqualified referenced type .
如果type-id的类型是对可能cv限定的类型的引用,则typeid表达式的结果将引用一个std :: type_info对象,该对象表示cv不限定的引用类型 。 If the type of the type-id is a class type or a reference to a class type, the class shall be completely-defined.
如果type-id的类型是类类型或对类类型的引用,则该类应完整定义。
Thus, typeid(int)
and typeid(int&)
will give the same result, although the two types are definitely different. 因此,尽管两种类型肯定不同,但
typeid(int)
和typeid(int&)
会给出相同的结果。 Similarly, for the type system int
and int const
are different types, but the typeid
operator ignores the const
qualification. 类似地,对于类型系统
int
和int const
是不同的类型,但是typeid
运算符忽略const
限定。 From Paragraph 5.2.7/5 of the C++11 Standard: 根据C ++ 11标准的5.2.7 / 5段:
The top-level cv-qualifiers of the glvalue expression or the type-id that is the operand of typeid are always ignored.
glvalue表达式或作为typeid操作数的type-id的顶级cv限定符始终被忽略。
Finally, int
and int*
are again different types for the type system, and the typeid
operator returns different results for them. 最后,对于类型系统,
int
和int*
仍然是不同的类型,并且typeid
运算符为它们返回不同的结果。
Type qualifiers, ( const
and volatile
), create different types. 类型限定符(
const
和volatile
)创建不同的类型。 int
is a different type from const int
. int
与const int
是不同的类型。
So do references, pointers, and arrays. 引用,指针和数组也是如此。 For example:
例如:
int
, int&
, int[10]
and int*
are all different types. int
, int&
, int[10]
和int*
都是不同的类型。
T
is a different type from std::remove_reference<T>::type
if T
is a reference. T
是由不同类型的std::remove_reference<T>::type
如果T
是一个参考。
The <typeinfo>
output of typeid(int).name()
is platform-dependent and doesn't have to distinguish between reference/non-reference types. typeid(int).name()
的<typeinfo>
输出是依赖于平台的,不必区分引用/非引用类型。 However, the C++ type system definitely distinguishes between T
and T&
, as you've discovered through type_traits
. 但是,正如您通过
type_traits
发现的那样,C ++类型系统绝对可以区分T
和T&
。
std::type_info::name
says nothing about identity. std::type_info::name
没有std::type_info::name
身份。 If you insist on using typeid
to test for identity, try the following: 如果您坚持使用
typeid
测试身份,请尝试以下操作:
assert(typeid(T) != typeid(U));
This is using the defined equality comparison operator on the type_info
objects. 这是在
type_info
对象上使用定义的相等性比较运算符 。 But prepare for disappointment: the above assertion will fail for T = int
and U = int&
because of §5.2.7/4 (see Andy's anser). 但是要为失望做准备:由于第5.2.7 / 4节,上述声明对于
T = int
和U = int&
将失败(请参阅Andy的分析工具)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.