I just started playing with metaprogramming and I am working on different tasks just to explore the domain. One of these was to generate a unique integer and map it to type,
I'm not aware of a way to map a compile-time constant integer to a type, but I can give you the next best thing. This example demonstrates a way to generate a unique identifier for a type which - while it is not an integral constant expression - will generally be evaluated at compile time. It's also potentially useful if you need a mapping between a type and a unique non-type template argument.
struct Dummy
{
};
template
struct TypeDummy
{
static const Dummy value;
};
template
const Dummy TypeDummy::value = Dummy();
typedef const Dummy* TypeId;
template::value>
struct TypePtr
{
static const TypeId value;
};
template
const TypeId TypePtr::value = p;
struct A{};
struct B{};
const TypeId typeA = TypePtr::value;
const TypeId typeB = TypePtr::value;
I developed this as a workaround for performance issues with ordering types using typeid(A) == typeid(B)
, which a certain compiler fails to evaluate at compile time. It's also useful to be able to store TypeId
values for comparison at runtime: e.g. someType == TypePtr::value