これには静的関数を使用して、そのクラスを継承できると思います。
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()); }
もちろん、それは静的なコンパイル時定数ではありません-自動的に可能だとは思いません(これらの型を手動でいくつかの型リストに追加する必要がありますmpl::vector
)。型が等しいかどうかを比較するだけで、これらすべてが必要ではないことに注意してください。is_same
コンパイル時定数を生成する(boostや他のライブラリにあり、書くのは簡単です)を使用するだけです
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]; }