I think you can just use a static function for this, and inherit its class:
struct IdCounter { static int counter; };
int IdCounter::counter;
template<typename Derived>
struct Id : IdCounter {
static int classId() {
static int id = counter++;
return id;
}
};
struct One : Id<One> { };
struct Two : Id<Two> { };
int main() { assert(One::classId() != Two::classId()); }
Of course, that won't be a static compile time constant - i don't think that is possible automatically (you would have to add those types manually to some type list like mpl::vector
). Please notice that for just comparing types for equality, you don't need all this. You just need to use is_same
(found in boost and other libraries and trivial to write) which yields a compile time constant
template<typename A, typename B>
struct is_same { static bool const value = false; };
template<typename A>
struct is_same<A, A> { static bool const value = true; };
int main() { char not_true[!is_same<One, Two>::value ? 1 : -1]; }