错误:对于从字符串文字中选择的字符,大小写标签没有减少为整数常量 [英] error: case label does not reduce to an integer constant for characters selected from a string literal

查看:76
本文介绍了错误:对于从字符串文字中选择的字符,大小写标签没有减少为整数常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用gcc 8.2编译此代码时,我收到此错误:

I get this error with this code compiling with gcc 8.2:

#define literal "string"
switch(i) {
case literal[0]:
break;
}

在我的示例中,编译器能否将表达式文字[0]减少为"s"?

Could the compiler reduce the expression literal[0] to 's' in my example?

推荐答案

6.6p6 .字符串文字可能不会出现在整数常量表达式中.

C2011 6.4.8.2p3 requires each case label's expression to be an integer constant expression. Integer constant expressions are a restricted subset of constant expressions, defined in §6.6p6. String literals may not appear in integer constant expressions.

6.4.8.2p3是一个约束"段落,因此该程序格式不正确.

6.4.8.2p3 is a "constraints" paragraph, so this program is ill-formed.

因此,从字面上看,您的问题的答案是:编译器可以"string" [0] 减少为,但不允许这样做;必须拒绝您的程序.

So the answer to your question, as literally asked, is: the compiler could reduce "string"[0] to 's', but it's not allowed to; it is required to reject your program instead.

这篇关于错误:对于从字符串文字中选择的字符,大小写标签没有减少为整数常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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