Reputation: 51
how would you create a kind of compile time indexer that given a group of enum classes is able to correctly create a unique identifier.
Template<class... Args>
struct Indexer
{
template<class T>
Indexer(T value)
{
value_ = someconstexprfunction<T>(value, interDistance_);
}
int enumInternalIndexCode() { /* ... */ };
int effectiveEnumCode() { /* ... */ }
static constexpr int enumDistance_ = 100;
int value_ = 0;
};
// Usage:
enum class A {a,b,c,d,e}; enum class B{ a1,b1,c1}; enum class C{z,y,q};
using MyIndexer = Indexer<A,B,C>;
MyIndexer(A::a) t1; // value_ == 0
MyIndexer(B::a1) t2; //value_ == 100
MyIndexer(B::b1) t3; //value_ == 101
MyIndexer(C::z) t4; //value_ == 200
t4.effectiveEnumCode(); // returns 0 (first element of the enum)
t4.enumInternalIndexCode(); // returns 2 (third enum in the Arg list (0,1,2) )
Ideally this should be able to work, or at least execute the hashing computations at compile time and even more ideally it should work in C++11. Is this feasible? thanks!
Upvotes: 0
Views: 88
Reputation: 48447
#include <type_traits>
#include <cstddef>
template <typename T, typename... Ts>
struct Index;
template <typename T, typename... Ts>
struct Index<T, T, Ts...>
: std::integral_constant<std::size_t, 0> {};
template <typename T, typename U, typename... Ts>
struct Index<T, U, Ts...>
: std::integral_constant<std::size_t, 1 + Index<T, Ts...>::value> {};
template <typename... Args>
class Indexer
{
public:
template <typename T>
constexpr Indexer(T value)
: _value(Index<T, Args...>::value * _enumDistance + static_cast<int>(value)) {}
constexpr int enumInternalIndexCode() const { return _value / _enumDistance; }
constexpr int effectiveEnumCode() const { return _value % _enumDistance; }
constexpr int value() const { return _value; }
private:
static constexpr int _enumDistance = 100;
int _value = 0;
};
Note, however, that this returns the enumerator value itself as an effective code.
Upvotes: 2