如何从枚举类中获取随机值?
您好,
最近进入C ++(确切地说是C ++ 14),我试图通过枚举来解决自己的方式,仍然有点麻烦
当前,试图从这样构建的枚举类中获取一个随机值:
enum class Niveau {
#define NIVEAU_DEF(NOM,VALEUR) NOM = VALEUR,
#include "niveau.def"
#undef NIVEAU_DEF
};
这是“ niveau.def”的内容,
NIVEAU_DEF(I, 1)
NIVEAU_DEF(II, 2)
...
NIVEAU_DEF(IX, 9)
NIVEAU_DEF(X, 10)
是否可以使用方法来拿起这些枚举中的一个随机枚举?还是这种构建枚举的方式不允许?
hello,
Been recently into C++ (C++14 to be precise), and I'me trying to get my way with enums, still have a bit of trouble figuring things out
Currently, trying to get a random value from an enum class that was constructed as such:
enum class Niveau {
#define NIVEAU_DEF(NOM,VALEUR) NOM = VALEUR,
#include "niveau.def"
#undef NIVEAU_DEF
};
And here is the content of "niveau.def"
NIVEAU_DEF(I, 1)
NIVEAU_DEF(II, 2)
...
NIVEAU_DEF(IX, 9)
NIVEAU_DEF(X, 10)
Is it possible to pick up a random one of those enumerations using a method ? Or does this way of constructing an enum doesn't allow it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您已经有了Xmacro,因此您可以将其重新使用以创建值的数组并从中挑选:
在语言的较新版本中,您可以将其作为
std :: array
的constexpr。 。Since you have a XMacro already, you can just reuse it to create an array of the values and pick from it:
In newer versions of the language, you can make it a nice constexpr with
std::array
.