我在DRBD软件的开源代码文件中注意到这一点(user/drbdtool_common.c)
const char* shell_escape(const char* s)
{
/* ugly static buffer. so what. */
static char buffer[1024];
char *c = buffer;
if (s == NULL)
return s;
while (*s) {
if (buffer + sizeof(buffer) < c+2)
break;
switch(*s) {
/* set of 'clean' characters */
case '%': case '+': case '-': case '.': case '/':
case '0' ... '9':
case ':': case '=': case '@':
case 'A' ... 'Z':
case '_':
case 'a' ... 'z':
break; …Run Code Online (Sandbox Code Playgroud) 我有一些静态const成员的声明
.H
class MyClass : public MyBase
{
public:
static const unsigned char sInvalid;
static const unsigned char sOutside;
static const unsigned char sInside;
//(41 more ...)
}
Run Code Online (Sandbox Code Playgroud)
的.cpp
const unsigned char MyClass::sInvalid = 0;
const unsigned char MyClass::sOutside = 1;
const unsigned char MyClass::sInside = 2;
//and so on
Run Code Online (Sandbox Code Playgroud)
在某些时候,我想在交换机中使用这些值,如:
unsigned char value;
...
switch(value) {
case MyClass::sInvalid : /*Do some ;*/ break;
case MyClass::sOutside : /*Do some ;*/ break;
...
}
Run Code Online (Sandbox Code Playgroud)
但是我得到以下编译器错误:错误:'MyClass :: sInvalid'不能出现在常量表达式中.
我已经阅读了其他开关 - …
我有一个final class Ring定义为:
final class Ring {
public static final int OUT = 3;
public static final int MID = 2;
public static final int IN = 1;
}
Run Code Online (Sandbox Code Playgroud)
我也有public class MorrisBoard以下代码:
public class MorrisBoard {
public static final Ring RING = new Ring();
private boolean checkMillBy(int ring, int x, int y) {
switch(ring) {
case MorrisBoard.RING.OUT:
//...
case MorrisBoard.RING.MID: //etc.
//...
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
MorrisBoard.RING.OUT引用一个在程序生命周期中不可变的变量.所有值都是最终的.
但是,我仍然收到以下错误:case expressions must be constant expressions.我对此感到困惑 …
今天,我偶然发现我的编译器在没有case的情况下在switch语句中编写代码时不会抱怨.(它确实抱怨缺少案例陈述,但在代码之后添加一个,没有多少警告让我知道代码是没用的.)
我试图弄清楚是否有允许以下代码的目的,或者它只是其中一个"限制它的更多工作,所以它是允许的".
#include <iostream>
void foo() {
std::cout << "foo" << std::endl;
}
int main()
{
for (int a = -10; a < 10; ++a)
{
switch(a)
{
foo();
case 4:
std::cout << "4" << std::endl;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,当a == 4时,这会按预期输出"4",并且它永远不会输出foo.所以问题是,是否有一些(可能是深奥但有用的)理由允许foo();在第一个案例之前发表声明?我知道我不能在那里声明和初始化变量.
(FWIW,我已经在几个编译器上对它进行了测试,它们都产生了相同的行为.令人惊讶的是,它们都没有输出警告.)
为什么java switch语句不能处理null,因为它有一个"default"子句?
例如,如果你有类似的东西
switch(value){
case VAL1: do_something1(); break;
case VAL2: do_something2(); break;
default: do_something3();
}
Run Code Online (Sandbox Code Playgroud)
不应该"默认"处理任何其他值,如null?
为什么我在下面的代码中得到" 并非所有代码路径返回值 " VeryBoolToBool()?
public enum VeryBool { VeryTrue, VeryFalse };
public bool VeryBoolToBool(VeryBool veryBool)
{
switch(veryBool)
{
case VeryBool.VeryTrue:
return true;
case VeryBool.VeryFalse:
return false;
// Un-commenting the unreachable(?) default clause will solve this
// default:
// throw new HowTheHellDidIGetHereException();
}
}
Run Code Online (Sandbox Code Playgroud)
编译器不能看到没有其他选项VeryBool吗?
我是Objective-C的新手,但是从我所知道的关于switch语句的所有内容来看,下面的代码不应该打印任何内容,因为对我来说似乎应该有一个编译错误.但是打印出2.我不懂为什么.有人可以解释一下吗?
- (void) test {
int myVar = 2;
switch (myVar) {
case 1:
{
NSLog(@"result: %d", myVar);
break;
case 2:
{
NSLog(@"result: %d", myVar);
}
break;
}
}
}
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,为什么变量i没有赋值1?
#include <stdio.h>
int main(void)
{
int val = 0;
switch (val) {
int i = 1; //i is defined here
case 0:
printf("value: %d\n", i);
break;
default:
printf("value: %d\n", i);
break;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我编译时,我会收到一个警告,即i尽管int i = 1;它已经初始化,但仍未进行初始化
$ gcc -Wall test.c
warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
printf("value %d\n", i);
^
Run Code Online (Sandbox Code Playgroud)
如果val = 0,那么输出是0.
如果val = 1或其他任何东西,那么输出也是0.
请向我解释为什么i声明变量但在开关内没有定义.标识符 …
是否有理由不能通过类型模式处理泛型变量?请考虑以下代码:
public static int CompareValues<T>(T left, T right)
{
switch (left)
{
case IComparable<T> comparableDif:
return comparableDif.CompareTo(right);
case System.Numerics.Complex c:
return c.Magnitude
.CompareTo(((System.Numerics.Complex)(object)right).Magnitude);
default:
throw new ArgumentException("unsupported type");
}
}
Run Code Online (Sandbox Code Playgroud)
IComparable接口上的第一个匹配是可以的,但第二个匹配不编译.为什么我必须使用这个拳击解决方法?
case object o when o is System.Numerics.Complex:
return ((System.Numerics.Complex)o).Magnitude
.CompareTo(((System.Numerics.Complex)(object)right).Magnitude);
Run Code Online (Sandbox Code Playgroud) 我正在编写C编译器,当我执行该switch语句时,一个约束使我很困惑。标准的 6.8.4.2p2节规定:
如果switch语句在标识符的范围内具有关联的大小写或默认标签,且标识符的类型可变,则整个switch语句应在该标识符的范围内。
带有脚注:
也就是说,声明要么在switch语句之前,要么在最后一个case或默认标签之后,该标签与包含该声明的块中的switch关联。
我真的不明白这个约束的含义。可以给我一个例子吗?
switch-statement ×10
c ×4
c# ×2
c++ ×2
declaration ×2
java ×2
c#-7.0 ×1
const ×1
enums ×1
final ×1
gcc ×1
generics ×1
goto ×1
label ×1
objective-c ×1