Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

typeid(T1) == typeid(T2) - is it correct ?

40 views
Skip to first unread message

porp...@gmail.com

unread,
Dec 22, 2015, 7:57:57 AM12/22/15
to
Hi

Is the following typeid comparison correct in all cases ?
typeid(T1) == typeid(T2);

or I should rather use:
typeid(T1).hash_code() == typeid(T2).hash_code()

thanks for help

Öö Tiib

unread,
Dec 22, 2015, 9:44:07 AM12/22/15
to
On Tuesday, 22 December 2015 14:57:57 UTC+2, porp...@gmail.com wrote:
> Hi
>
> Is the following typeid comparison correct in all cases ?
> typeid(T1) == typeid(T2);

Yes. The 'std::type_info::operator==' must return true if the T1 and
T2 are same type and false if those are not.

>
> or I should rather use:
> typeid(T1).hash_code() == typeid(T2).hash_code()

That is worse. We may only trust it when it results with 'false',
when it results with 'true' then we have to make further checks if
it isn't false positive. Avoid it unless it is some sort of performance
optimization.

You perhaps were worried that the 'std::type_info' objects referred
by 'typeid' may be different so addresses may not compare equal:

const std::type_info& ti1 = typeid(A);
const std::type_info& ti2 = typeid(A);
assert(&ti1 == &ti2); // it is not guaranteed that addresses are same

Do not compare addresses of objects what 'typeid' refers and all is fine.

Richard

unread,
Dec 22, 2015, 1:33:47 PM12/22/15
to
[Please do not mail me a copy of your followup]

porp...@gmail.com spake the secret code
<e16c168f-ff72-4a9b...@googlegroups.com> thusly:

>Is the following typeid comparison correct in all cases ?
>typeid(T1) == typeid(T2);

An alternative is to use std::is_same<T1, T2>::value.
<http://en.cppreference.com/w/cpp/types/is_same>
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Alf P. Steinbach

unread,
Dec 22, 2015, 7:33:50 PM12/22/15
to
On 12/22/2015 7:33 PM, Richard wrote:
> [Please do not mail me a copy of your followup]
>
> porp...@gmail.com spake the secret code
> <e16c168f-ff72-4a9b...@googlegroups.com> thusly:
>
>> Is the following typeid comparison correct in all cases ?
>> typeid(T1) == typeid(T2);
>
> An alternative is to use std::is_same<T1, T2>::value.
> <http://en.cppreference.com/w/cpp/types/is_same>

Your alternative std::is_same more likely to be the practical choice.
For with a few exceptions, checking the identity of two types only makes
sense in template code, and there it's ungood to have the result only at
run-time. std::is_same provides a compile time result.

In the cases where one really does want to compare std::type_info
instances, using std::type_index is usually preferable, because
std::type_info lacks a "operator<" named as such.

With a pre-C++11 compiler a class like std::type_index can be easily
defined, and indeed Andre Alexandrescu defined one in his classic book
"Modern C++ Design" (still worth reading, I think).

Cheers,

- Alf

Öö Tiib

unread,
Dec 23, 2015, 4:17:20 AM12/23/15
to
On Wednesday, 23 December 2015 02:33:50 UTC+2, Alf P. Steinbach wrote:
> On 12/22/2015 7:33 PM, Richard wrote:
> > [Please do not mail me a copy of your followup]
> >
> > porp...@gmail.com spake the secret code
> > <e16c168f-ff72-4a9b...@googlegroups.com> thusly:
> >
> >> Is the following typeid comparison correct in all cases ?
> >> typeid(T1) == typeid(T2);
> >
> > An alternative is to use std::is_same<T1, T2>::value.
> > <http://en.cppreference.com/w/cpp/types/is_same>
>
> Your alternative std::is_same more likely to be the practical choice.
> For with a few exceptions, checking the identity of two types only makes
> sense in template code, and there it's ungood to have the result only at
> run-time. std::is_same provides a compile time result.

On most cases when I have seen 'typeid' result compared it was design
defect or code smell ... like missing virtual function or missing visitor
pattern. For some people visitor pattern is hard to grasp but I have never
observed typeid-comparing code being easier to maintain for such people.
However sometimes 'typeid' is a good choice, particularly when involved
types may be polymorphic pointers from different inheritance trees or
when 'name' is also needed for something (say code generation).

>
> In the cases where one really does want to compare std::type_info
> instances, using std::type_index is usually preferable, because
> std::type_info lacks a "operator<" named as such.
>
> With a pre-C++11 compiler a class like std::type_index can be easily
> defined, and indeed Andre Alexandrescu defined one in his classic book
> "Modern C++ Design" (still worth reading, I think).

You are correct that 'std::type_index' is simpler to use. Lack of 'operator<'
can be compensated with 'std::type_info::before'. Bigger problem is that
'std::type_info' is not copyable and so can't be stored. 'std::type_index'
deals with it more safely (and sometimes more efficiently) than raw
pointers to 'std::type_info'.

Merry Christmas
0 new messages