I'm trying to do the following:
template <typename type>
struct GetUniqueTypeValue
{
constexpr static void* get()
{
return (void*)&GetUniqueTypeValue::get;
}
};
constinit static void* record_array[16]{};
constexpr void * sequentialise(void* type_value)
{
return nullptr; // I want to return a seuqntialised number here.
}
int main()
{
constexpr void* type_value = GetUniqueTypeValue<int>::get();
}
I understand the value from typeid()
and type_index
aren't compile-time values, so that's why I've used the function pointer method, which should be guaranteed to be unique, right? However, I'm stuck at this part. What I want is basically a sequential counter for type IDs, the way that's commonly done at runtime using certain tricks, like just incrementing a counter each time a getID()
function is called, or when a static
object is initialised at startup. Except I want it to be compile-time.
How can I do this? Edit: What I mean is without feeding each type into a function. I mean like this trick:
struct SequentialiseTypes
{
static inline int counter = 0;
template <typename T>
int getID() { return counter++; }
}
or the other way as:
int counter = 0;
template <typename T>
int ID = counter++;
except at compile time