For example, I want to implement a template function like this:
template<typename T>
int GetTypeIndex() {}
which always return a determinant zero-based index for the same 'T', and finally a method to get the total count of all 'T's at compile time (a constexpr or macro?)
struct A
{
};
struct B
{
};
GetTypeIndex<A>();// returns 0,
GetTypeIndex<B>();// returns 1,
size_t sizeTable[ALL_TYPE_COUNT]; //ALL_TYPE_COUNT equals to 2 hence GetTypeIndex has 2 specialization(A and B)
I've already made it by pre-declare every 'T' with some macro wraps the __COUNTER__, but it's just not that convenient, and use __COUNTER__ in other place will corrupt the whole design.
I just want to get rid of those macro tricks.
Edit: My English is poor so I decide to put on what I made previously with macros to help understanding what I mean.
#define INDEXED_TYPE_DEFINE_BEGIN \
template<typename T> int GetTypeIndex() = delete;
#define INDEXED_TYPE_DEFINE_END \
constexpr int INDEXED_TYPE_COUNT = __COUNTER__;
#define AS_INDEXED_TYPE(T) T\
constexpr int Index_##T= __COUNTER__;\
template<> inline int GetTypeIndex<T>() { return Index_##T; }
and the user code would be like:
INDEXED_TYPE_DEFINE_BEGIN
typedef struct
{
float x;
float y;
}
AS_INDEXED_TYPE(Position)
typedef struct
{
float min;
float max;
}
AS_INDEXED_TYPE(Range)
INDEXED_TYPE_DEFINE_END
now
GetTypeIndex<Position>() would return 0
GetTypeIndex<Range>() would return 1
INDEXED_TYPE_COUNT equals to 2 and I can use it at compile time
but it's just very inconvenient.
It might be easier to just keep a single list of all types:
https://godbolt.org/z/nr6v14Gaz
This doesn't need a macro but forces you to declare all your types in one place (they can't be piecemeal), but it sounds like you are already doing that.