枚举中的元素数量
在 C 中,有没有一种很好的方法来跟踪枚举中的元素数量? 我见过
enum blah {
FIRST,
SECOND,
THIRD,
LAST
};
但这只有在项目是连续的并且从零开始时才有效。
In C, is there a nice way to track the number of elements in an enum? I've seen
enum blah {
FIRST,
SECOND,
THIRD,
LAST
};
But this only works if the items are sequential and start at zero.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您不分配枚举,您可以执行以下操作:
NumberOfTypes 将计算为 3,这是实际类型的数量。
If you don't assign your enums you can do somethings like this:
NumberOfTypes will evaluate to 3 which is the number of real types.
我不相信有。 但是,如果这样的数字不是连续的,并且您还没有在某处拥有它们的列表,您会如何处理它们呢? 如果它们是连续的但以不同的数字开始,您始终可以这样做:
I don't believe there is. But what would you do with such a number if they are not sequential, and you don't already have a list of them somewhere? And if they are sequential but start at a different number, you could always do:
老问题,我知道。 这是针对有同样问题的谷歌用户的。
您可以使用 X-Macros
示例:
这对于 IDE 来说也是透明的,因此会自动完成会工作得很好(因为这一切都是在预处理器中完成的)。
Old question, I know. This is for the googlers with the same question.
You could use X-Macros
Example:
This is also transparent to an IDE, so auto-completes will work fine (as its all done in the pre-processor).
很不幸的是,不行。 那没有。
Unfortunately, no. There is not.
我知道这是一个非常古老的问题,但由于接受的答案是错误的,我觉得有必要发布自己的答案。 我将重用已接受的答案的示例,稍加修改。
(假设枚举是连续的。)
任何开发人员都知道原因:
count = last - first + 1
。这适用于任何符号组合(两端都是负数,都是正数,或者只有一端是负数)。 你可以试试。
编辑:再次阅读文本,我有一个疑问。
END
是否意味着不属于所提供项目的一部分? 这对我来说看起来很奇怪,但好吧,我想这可能是有道理的......I know this is a very old question, but as the accepted answer is wrong, I feel compelled to post my own. I'll reuse the accepted answer's example, slightly modified.
(Making the assumption that enums are sequential.)
Any developer knows the reason:
count = last - first + 1
.And this works with any combination of signs (both ends negative, both positive, or only first end negative). You can try.
Edit: reading the text again, I got a doubt. Is that
END
meant not to be part of the offered items? That looks weird to me, but well, I guess it could make sense...好吧,由于枚举无法在运行时更改,因此您可以做的最好的事情是:
但我发现很难想象该信息会派上用场的情况。 你到底想做什么?
Well, since enums can't change at run-time, the best thing you can do is:
But I find it difficult to envisage a situation where that information would come in handy. What exactly are you trying to do?
嗯,这已经很古老了,但是我找到了这里没有提到的方法。
大多数编译器都提供了 __COUNTER__ 宏。 该宏最初扩展为 0,每当使用它时,它都会给自身加 1。 它在目标文件的开头重置。 这意味着您可以这样写:
这将打印:
这具有不将最后一个变体用作计数器的优点,这可能会弄乱 switch 语句。 缺点是,正如您所看到的,它使得很难推断枚举变体的实际值。 只要您不需要它们的明确值,就可以了。 例如,如果您将
Test
放入test.h
中,并将Test2
放入test2.h
中,则调用 include 语句的顺序会更改枚举变体的值。 它不会更改TestSize
和TestSize2
的值,因为它们是根据与__COUNTER__
初始值的差来定义的。Well, this is very old, but I have found a method not mentioned here.
Most compilers provided the
__COUNTER__
macro. This macro initially expands to 0, and whenever it is used it adds 1 to itself. It is reset at the beginning of object files. This means that you can write this:This will print:
This has the advantage of not having the last variant used as counter, which can mess up switch statements. The downside is that, as you can see, it makes it very hard to reason about the actual value of the enum variants. As long as you don't need their explicit value, you're fine. For example, if you place
Test
in, saytest.h
, andTest2
intest2.h
, the order with which you call the include statements changes the value of the enum variants. It does not change the value ofTestSize
andTestSize2
because they are defined in terms of the difference with the initial value of__COUNTER__
.