在C ++中将int转换为枚举的通用方法 [英] Generic way to cast int to enum in C++
问题描述
在 如果 背景:我有一个外部枚举类型和无法控制。 显而易见的是注释你的枚举:/ p> 您需要使用 无论如何,你是对比7.2 / 6: 对于枚举,其中emin是 如果您不是 Is there a generic way to cast If Background: I have an external enum type and no control over the source code. I'd like to store this value in a database and retrieve it. The obvious thing is to annotate your enum: You need the array to be kept up to date with In any case, you're up against 7.2/6: For an enumeration where emin is the
smallest enumerator and emax is the
largest, the values of the enumeration
are the values of the underlying type
in the range bmin to bmax, where bmin
and bmax are, respectively, the
smallest and largest values of the
smallest bit-field that can store emin
and emax. It is possible to define an
enumeration that has values not
defined by any of its enumerators. So if you aren't the author of 这篇关于在C ++中将int转换为枚举的通用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! C ++ $中是否有一种通用的方法来将
int
转换为枚举
c $ c>?
int
在枚举
它应该返回一个枚举
值,否则抛出一个异常
。有没有办法
//通用代码
#include< algorithm>
template< typename T>
struct enum_traits {};
template< typename T,size_t N>
T * endof(T(& ra)[N]){
return ra + N;
}
template< typename T,typename ValType>
T check(ValType v){
typedef enum_traits< T>性状
const T * first = traits :: enumerators;
const T * last = endof(traits :: enumerators);
if(traits :: sorted){//可能过早优化
if(std :: binary_search(first,last,v))return T(v);
} else if(std :: find(first,last,v)!= last){
return T(v);
}
throwexception
}
//枚举的增强定义
枚举e {
x = 1,
y = 4,
z = 10,
};
模板<>
struct enum_traits< e> {
static const e enumerators [];
static const bool sorted = true;
};
//必须出现在一个TU中,
//所以如果上面的是在一个头,那么它将需要数组大小
const e enum_traits< e> :: enumerators [] = {x,y,z};
//用法
int main(){
e good = check< e>(1);
e bad = check< e>(2);
}
e
,如果你不是 e
的作者,这是一个麻烦。如Sjoerd所说,它可能是任何体面的构建系统自动化。
最小枚举值,emax是
最大值,枚举值
的值是值在范围bmin到bmax中的基本类型
的值bmin,其中bmin
和bmax分别是可以存储emin的
最小位字段的
最小和最大值
和emax。可以定义一个
枚举,其枚举值不是由其任何枚举器定义的
。
e
的作者,您可能或可能不保证 e
的有效值实际上出现在其定义中。int
to enum
in C++
?int
falls in range of an enum
it should return an enum
value, otherwise throw an exception
. Is there a way to write it generically? More than one enum type
should be supported.// generic code
#include <algorithm>
template <typename T>
struct enum_traits {};
template<typename T, size_t N>
T *endof(T (&ra)[N]) {
return ra + N;
}
template<typename T, typename ValType>
T check(ValType v) {
typedef enum_traits<T> traits;
const T *first = traits::enumerators;
const T *last = endof(traits::enumerators);
if (traits::sorted) { // probably premature optimization
if (std::binary_search(first, last, v)) return T(v);
} else if (std::find(first, last, v) != last) {
return T(v);
}
throw "exception";
}
// "enhanced" definition of enum
enum e {
x = 1,
y = 4,
z = 10,
};
template<>
struct enum_traits<e> {
static const e enumerators[];
static const bool sorted = true;
};
// must appear in only one TU,
// so if the above is in a header then it will need the array size
const e enum_traits<e>::enumerators[] = {x, y, z};
// usage
int main() {
e good = check<e>(1);
e bad = check<e>(2);
}
e
, which is a nuisance if you're not the author of e
. As Sjoerd says, it can probably be automated with any decent build system.
e
, you may or may not have a guarantee that valid values of e
actually appear in its definition.