问题描述
有支持范围的情况下在C(99?)或客观C开关语句是一个黑客?
我知道这是不支持这样写的:
Is there a hack to support range case in a c(99?) or objective C switch statement ?I know this is not supported to write something like this:
switch(x)
case 1:
case 2..10:
case 11:
不过,我想应该有产生code用#define宏的方式。当然
我可以定义一个宏案件的名单,但我希望有更优雅的方式一样
CASERANGE(X,X + 10),这将产生
But I was thinking there should be a way to generate code with a #define macro. Of courseI can define a macro with the list of cases but I was hoping for a more elegant way likeCASERANGE(x,x+10) which would generate:
case x
case x+1
case x+2
是它甚至可能吗?
is it even possible ?
推荐答案
GCC拥有的,允许类似于您的第一个例子中的东西,但除此之外,如果有这样做的一个便携式/ ANSI方式,它会被完事了。我不相信有一个。
GCC has an extension to the C language that allows something similar to your first example, but other than that, if there was a portable/ANSI way of doing it, it would have been done by now. I don't believe there is one.
这篇关于Simulater /生成switch语句范围在C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!