paul
paul

Reputation: 661

C++ constexpr type ID

I notice that the typeid(T) call provides a nice constexpr-compatible way to retrieve a unique identifier for a given class at compile-time. This is very useful, but I notice that relational operators between the type_info objects which this yields are not enabled at compile-time. This makes sense, since the actual IDs may vary at runtime. However, I would like to impose an ordering between types such that:

  1. Every type has some unique ID which is generated at compile-time. It does not matter what these IDs are, or how they are ordered between classes.
  2. For each pair of types, the IDs between the types can be compared relationally to determine which type has the "lesser" ID.
  3. It does not matter if the order of the IDs changes between compilations.

This is very doable at compile-time by explicitly defining the IDs for each type. However, I was wondering if there is a more reasonable approach to generate compile-time, constexpr-compatible IDs per type which allow for relational operations.

Thanks!

Upvotes: 1

Views: 1422

Answers (1)

ildjarn
ildjarn

Reputation: 62985

Summarizing comments:

boost::fusion::set<> has the functionality you need, with metafunctions to mimic most algorithms in the standard library, including insertion and removal. All Fusion containers are completely stack-based, as all are simply tuples with algorithm metafunctions on top.

(Boost.Fusion overview here.)

Upvotes: 2

Related Questions