有没有办法找到 C++ 中枚举的基数(大小)?

发布于 2024-08-20 02:49:14 字数 104 浏览 3 评论 0原文

可以编写一个返回枚举中元素数量的函数吗?例如,假设我定义了:

enum E {x, y, z};

那么 f(E) 将返回 3。

Could one write a function that returns the number of elements in an enum? For example, say I have defined:

enum E {x, y, z};

Then f(E) would return 3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

姜生凉生 2024-08-27 02:49:14

没有。

如果有的话,你不会看到这么多像这样的代码:

enum E {
  VALUE_BLAH,
  VALUE_OTHERBLAH,
  ...
  VALUE_FINALBLAH,
  VALUE_COUNT
}

请注意,此代码也是一个(讨厌的)解决方案的提示 - 如果你添加一个最终的“guard”元素,并且不明确声明枚举字段,那么最后一个“COUNT”元素将具有您要查找的值——发生这种情况是因为枚举计数是从零开始的:

enum  B {
  ONE,   // has value = 0
  TWO,   // has value = 1
  THREE, // has value = 2
  COUNT  // has value = 3 - cardinality of enum without COUNT
}

Nope.

If there were, you wouldn't see so much code like this:

enum E {
  VALUE_BLAH,
  VALUE_OTHERBLAH,
  ...
  VALUE_FINALBLAH,
  VALUE_COUNT
}

Note that this code is also a hint for a (nasty) solution -- if you add a final "guard" element, and don't explicitly state the values of the enum fields, then the last "COUNT" element will have the value you're looking for -- this happens because enum count is zero-based:

enum  B {
  ONE,   // has value = 0
  TWO,   // has value = 1
  THREE, // has value = 2
  COUNT  // has value = 3 - cardinality of enum without COUNT
}
我ぃ本無心為│何有愛 2024-08-27 02:49:14

有很多方法,但你必须工作......一点:)

基本上你可以用宏来获得它。

DEFINE_NEW_ENUM(MyEnum, (Val1)(Val2)(Val3 = 6));

size_t s = count(MyEnum());

它是如何运作的?

#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/size.hpp>

#define DEFINE_NEW_ENUM(Type_, Values_)\
  typedef enum { BOOST_PP_SEQ_ENUM(Values_) } Type_;\
  size_t count(Type_) { return BOOST_PP_SEQ_SIZE(Values_); }

请注意,长度也可以是模板专业化或任何东西。我不了解你,但我真的很喜欢 BOOST_PP 中“序列”的表现力;)

There are ways, but you have to work... a bit :)

Basically you can get it with a macro.

DEFINE_NEW_ENUM(MyEnum, (Val1)(Val2)(Val3 = 6));

size_t s = count(MyEnum());

How does it work ?

#include <boost/preprocessor/seq/enum.hpp>
#include <boost/preprocessor/seq/size.hpp>

#define DEFINE_NEW_ENUM(Type_, Values_)\
  typedef enum { BOOST_PP_SEQ_ENUM(Values_) } Type_;\
  size_t count(Type_) { return BOOST_PP_SEQ_SIZE(Values_); }

Note that length could also be a template specialization or anything. I dont know about you but I really like the expressiveness of a "Sequence" in BOOST_PP ;)

゛时过境迁 2024-08-27 02:49:14

不,这是一个 VFAQ,答案是否定的!

无论如何,并非没有拼凑。

即使最后一个条目的技巧也只有在所有值都不是非默认的情况下才有效。例如,

enum  B {
         ONE,   // has value = 0
         TWO,   // has value = 1
         THREE=8, // because I don't like threes
         COUNT  // has value = 9 
        }

No, this is a VFAQ and the answer is NO!!

Not without kludging anyway.

Even that trick about with a final entry only works if none of the values are non-default. E.g.,

enum  B {
         ONE,   // has value = 0
         TWO,   // has value = 1
         THREE=8, // because I don't like threes
         COUNT  // has value = 9 
        }
埋葬我深情 2024-08-27 02:49:14

不。一方面,您不能将类型作为参数(只能是类型的实例)

No. For one thing, you can't take types as parameters (just instances of types)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文