【发布时间】:2020-09-27 04:35:20
【问题描述】:
如果给定一组枚举类,您将如何创建一种编译时索引器,它能够正确创建唯一标识符。
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) )
理想情况下,这应该能够工作,或者至少在编译时执行散列计算,更理想的是它应该在 C++11 中工作。这可行吗?谢谢!
【问题讨论】:
-
effectiveEnumCode是不可能的,你能得到的只是枚举值本身。 -
为什么不呢?我知道如果我使用额外的内存并在构建时保存它可以得到它,但理想的情况是在这个结构中只有 value_ ..也许使用带有枚举大小信息的外部特征?
-
喜欢here ?
-
仅当对象为
constexpr
标签: c++ c++11 templates template-meta-programming constexpr