Created
March 27, 2021 13:50
-
-
Save ThirdPartyNinjas/660efbaa02868941b6bbbbf73c3039e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Note: Experimental code, probably not production useful! | |
// Get a unique identifier based on the type, without modifying the type | |
// This will be unique for the runtime of the program, but is based on execution | |
// order, so it will possibly change the next time the program runs. | |
// Don't serialize it! | |
using UniqueIdType = uint64_t; | |
struct UniqueIdBase | |
{ | |
inline static UniqueIdType globalId = 0; | |
}; | |
template <typename T> | |
struct UniqueId : public UniqueIdBase | |
{ | |
const static UniqueIdType Id() | |
{ | |
const static UniqueIdType localId = ++UniqueIdBase::globalId; | |
return localId; | |
} | |
}; | |
template <typename T> | |
UniqueIdType GetUniqueId() | |
{ | |
return UniqueId<T>::Id(); | |
} | |
// Examples (these are the same): | |
// UniqueIdType id = GetUniqueId<int>(); | |
// id = UniqueId<int>::Id(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment