Reputation: 951
Are int&
and int
the same type? if I use is_same<int,int&>::value
i get false
but typeid(int).name() == typeid(int&).name()
are the same?
secondly the same question for int
and const int
?
Thirdly int
and 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.
Keen to get some good commentary on this.
Upvotes: 0
Views: 214
Reputation: 126432
From Paragraph 5.2.7/4 of the C++11 Standard:
When typeid is applied to a type-id, the result refers to a std::type_info object representing the type of the type-id. 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. If the type of the type-id is a class type or a reference to a class type, the class shall be completely-defined.
Thus, typeid(int)
and typeid(int&)
will give the same result, although the two types are definitely different. Similarly, for the type system int
and int const
are different types, but the typeid
operator ignores the const
qualification. From Paragraph 5.2.7/5 of the C++11 Standard:
The top-level cv-qualifiers of the glvalue expression or the type-id that is the operand of typeid are always ignored.
Finally, int
and int*
are again different types for the type system, and the typeid
operator returns different results for them.
Upvotes: 4
Reputation: 53289
Type qualifiers, (const
and volatile
), create different types. int
is a different type from const int
.
So do references, pointers, and arrays. For example:
int
, int&
, int[10]
and int*
are all different types.
T
is a different type from std::remove_reference<T>::type
if T
is a reference.
The <typeinfo>
output of typeid(int).name()
is platform-dependent and doesn't have to distinguish between reference/non-reference types. However, the C++ type system definitely distinguishes between T
and T&
, as you've discovered through type_traits
.
Upvotes: 2
Reputation: 545588
std::type_info::name
says nothing about identity. If you insist on using typeid
to test for identity, try the following:
assert(typeid(T) != typeid(U));
This is using the defined equality comparison operator on the type_info
objects. 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).
Upvotes: 0