Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is typeid the same on different computers? [duplicate]

Tags:

c++

I am wondering if hash_code() of typeid is the same on different computers? For an example: if typeid(int).hash_code() is it will be the same on another computer?

like image 942
Vento Avatar asked Apr 07 '20 13:04

Vento


2 Answers

I am wondering if hash_code() of typeid is the same on different computers?

It may or may not be. From cppreference the behavior is:

Returns an unspecified value such that for all type_info objects referring to the same type, their hash_code() is the same.

No other guarantees are given: type_info objects referring to different types may have the same hash_code (although the standard recommends that implementations avoid this as much as possible), and hash_code for the same type can change between invocations of the same program.

like image 142
NathanOliver Avatar answered Sep 29 '22 07:09

NathanOliver


From cppreference :

hash_code for the same type can change between invocations of the same program

(cited from here).
So not only is it not guaranteed to be the same on different computers, it's not even guaranteed to be the same for the same program every time.

like image 33
melk Avatar answered Sep 29 '22 08:09

melk