Skip to content

Instantly share code, notes, and snippets.

@ThirdPartyNinjas
Created March 27, 2021 13:50
Show Gist options
  • Save ThirdPartyNinjas/660efbaa02868941b6bbbbf73c3039e9 to your computer and use it in GitHub Desktop.
Save ThirdPartyNinjas/660efbaa02868941b6bbbbf73c3039e9 to your computer and use it in GitHub Desktop.
// 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