C / C ++语言提议:从“整数常量表达式”中更改“案例表达式”。 “整体表达” [英] C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression"

查看:71
本文介绍了C / C ++语言提议:从“整数常量表达式”中更改“案例表达式”。 “整体表达”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C / C ++语言提案:

从整数常量表达式中更改案例表达式到积分表达式


C ++标准(ISO / IEC 14882,第二版,2003-10-15)

在6.4.2下说( 2)[另见5.19]:


case constant-expression:


我建议switch语句的case表达式

可以从整数常量表达式更改到积分表达式。

这开启了许多新的可能性,从那时起函数调用

将被允许在案例表达式中。

旧的案例将继续发挥作用,因为

它是新案例的一部分。


示例用法:


// ...

int f()

{

// ...

返回BLA1;

}


int g()

{

// .. 。

返回BLA2;

}


int h()

{

// ...

返回BLA3;

}


int y,x = f();

开关(x)

{

案例123:y = g();休息;

案例g():y = 456;打破; //使用新案例功能,即。 func-call

case h():y = 789;打破; //同上

默认值:y = -1;休息;

}

C/C++ language proposal:
Change the ''case expression'' from "integral constant-expression" to "integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:

case constant-expression :

I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".
This opens up many new possibilities since then also function calls
would be permitted in the case expression.
The old case case would continue to function since
it is a subset of the new case case.

Example usage:

//...
int f()
{
//...
return BLA1;
}

int g()
{
//...
return BLA2;
}

int h()
{
//...
return BLA3;
}

int y, x = f();
switch (x)
{
case 123 : y = g(); break;
case g() : y = 456; break; // using new case feature, ie. func-call
case h() : y = 789; break; // ditto
default : y = -1; break;
}

推荐答案

2008年10月23日14:17,在comp.lang中。 c,Adem(fo***********@alicewho.com)

写道:
On October 23, 2008 14:17, in comp.lang.c, Adem (fo***********@alicewho.com)
wrote:

C / C ++语言提议:

从'integral constant-expression'更改''case expression''

积分表达式


C ++标准(ISO / IEC 14882,第二版,2003-10-15)

说6.4.2(2)[另见5.19]:
C/C++ language proposal:
Change the ''case expression'' from "integral constant-expression" to
"integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:



[snip]


对不起,但是你的提案是comp.lang.c新闻组的主题。我怀疑它对comp.lang.c ++来说也是偏离主题的。


因为C和C ++是两种不同的语言,每种语言都是由他们自己定义的

标准,我建议你打破你的C / C ++标准。建议改为C

提案和单独的C ++提案,并将每个提交给他们的

各自的标准机构。


对于C,您/可能/将您的提案发布到comp.std.c新闻组,因为

是讨论C

语言标准的后果和增强的小组。


-

Lew Pitcher


Master Code Code& JOAT-in-training |已注册的Linux用户#112576
http://pitcher.digitalfreehold.ca/ |可根据要求提供GPG公钥

---------- Slackware - 因为我知道我在做什么。 ------

[snip]

Sorry, but your proposal is off-topic for the comp.lang.c newsgroup. I
suspect that it is also off-topic for comp.lang.c++.

As C and C++ are two separate languages, each defined by their own
standards, I suggest that you break your "C/C++" proposal into a "C
proposal" and a separate "C++ proposal", and submit each to their
respective standards bodies.

For C, you /might/ post your proposal to the comp.std.c newsgroup, as that
is the group that discusses the ramifications of and enhancements to the C
language standard.

--
Lew Pitcher

Master Codewright & JOAT-in-training | Registered Linux User #112576
http://pitcher.digitalfreehold.ca/ | GPG public key available by request
---------- Slackware - Because I know what I''m doing. ------


Adem写道:
Adem wrote:

C / C ++语言提案:

从整数常量表达式中更改案例表达式到积分表达式


C ++标准(ISO / IEC 14882,第二版,2003-10-15)

在6.4.2下说( 2)[另见5.19]:


case constant-expression:


我建议switch语句的case表达式

可以从整数常量表达式更改到积分表达式。
C/C++ language proposal:
Change the ''case expression'' from "integral constant-expression" to "integral expression"

The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:

case constant-expression :

I propose that the case expression of the switch statement
be changed from "integral constant-expression" to "integral expression".



switch(rand()){

case rand():puts(" What''s"); break;

case rand():puts(" wrong);; break;

case rand():puts(" with"); break;

case rand():puts(" this"); break;

case rand():puts(" picture?");休息;

}


-
Er ********* @ sun.com


Eric Sosman< Er ********* @ sun.comwrites:
Eric Sosman <Er*********@sun.comwrites:

Adem写道:
Adem wrote:

> C / C ++语言提案:改变' 'case expression''来自
整数常量表达式 积分表达
C ++标准(ISO / IEC 14882,第二版,2003-10-15)
根据6.4.2(2)[另见5.19]说:
case constant-expression:我建议将switch语句的case表达式
改为integral constant-expression。到积分表达式。
>C/C++ language proposal: Change the ''case expression'' from
"integral constant-expression" to "integral expression"
The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15)
says under 6.4.2(2) [see also 5.19]:
case constant-expression : I propose that the case expression
of the switch statement
be changed from "integral constant-expression" to "integral expression".



switch(rand()){

case rand():puts(" What''s"); break;

case rand():puts(" wrong);; break;

case rand():puts(" with"); break;

case rand():puts(" this"); break;

case rand():puts(" picture?");休息;

}


switch (rand()) {
case rand(): puts ("What''s"); break;
case rand(): puts ("wrong"); break;
case rand(): puts ("with"); break;
case rand(): puts ("this"); break;
case rand(): puts ("picture?"); break;
}



好​​吧,* *可以定义为等同于以下内容:


{

const int __tmp = rand();

if(__tmp == rand()puts(What's's);

else if(__tmp == rand()puts(" wrong");

else if(__tmp == rand()puts(" with");

else if(__tmp == rand()puts(" this");

else if(__tmp == rand()puts(" picture?");

}


这个例子只是为了这个例子,而不是

定义;其他转换会当一个案件

落到下一个案件时被要求。我拒绝考虑这将如何影响Duff'的设备。

C'的switch语句的一点是,它可以作为跳转表有效地实现
,与if / else链相比,
$ b必须对$ b条件进行评估最后。


如果你想做一些类似switch语句的东西,但

的情况值不是常数,你总是可以写一个if / else

chain。如果你想用语言支持这种事情,那么你只需要指定一次比较的LHS,我想

我更喜欢引入一个新的构造,而不是将这个

功能添加到现有的switch / case构造中。使用这个

提议,将单个表达式从常量更改为

非常量可能会对生成的代码产生重大影响,并且

取决于它的定义方式,对代码行为的微妙影响。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

Nokia

我们必须做点什么。这是事情。因此,我们必须这样做。

- Antony Jay和Jonathan Lynn,是部长

Well, it *could* be defined to be equivalent to the following:

{
const int __tmp = rand();
if (__tmp == rand() puts("What''s");
else if (__tmp == rand() puts("wrong");
else if (__tmp == rand() puts("with");
else if (__tmp == rand() puts("this");
else if (__tmp == rand() puts("picture?");
}

That''s intended to be equivalent code just for this example, not a
definition; other transformations would be required when one case
falls through to the next. And I refuse to think about how this would
affect Duff''s Device.

The point of C''s switch statement is that it can be implemented
efficiently as a jump table, in contrast to an if/else chain where the
conditions have to be evaluated sequentially.

If you want to do something like a switch statement, but where the
case values aren''t constant, you can always just write an if/else
chain. If you want to support this kind of thing in the language, so
that you only have to specify the LHS of the comparison once, I think
I''d prefer to introduce a new construct rather than adding this
functionality to the existing switch/case construct. With this
proposal, changing a single expression from a constant to a
non-constant could have substantial effects on the generated code, and
depending on how it''s defined, subtle effects on the code''s behavior.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"


这篇关于C / C ++语言提议:从“整数常量表达式”中更改“案例表达式”。 “整体表达”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆