validate integer是一些枚举类项(C ++ 11) [英] validate integer is some enum class item (C++11)
问题描述
我有一些枚举类
enum class Foo { A=1, B=18 , Z=42 };
我想检查一些整数是否可以转换为 Foo
。
什么是理想的方式这样做?这是为运行时检查(整数在编译时还未知)
i want to check if some integer can be converted into a Foo
.
What would be the ideal way to do this? this is for runtime check (the integer is not known yet at compile-time)
显然,我可以这样做的方式(写一个函数 bool CheckEnum(Foo);
有一个大屁股开关返回true除了默认的所有情况),但我希望一个更优雅的机制,避免了这么多的写作。 MPL或 Boost.Preprocessor
将是一个完全可以接受的解决方案,但其中一个我不知道很少关于
Obviously i can do this the hard way (write a function bool CheckEnum(Foo);
with a big-ass switch returning true for all cases except the default one), but i was hoping a more elegant mechanism that avoided so much writing. MPL or Boost.Preprocessor
would be a perfectly acceptable solution, but one of which i sadly know very little about
推荐答案
解决这个问题的办法是删除枚举,并用一些数组替换它们,使用 XMACROs。
A solution to this problem is to ditch the enums, and replace it with some arrays which are created using XMACROs.
这篇关于validate integer是一些枚举类项(C ++ 11)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!