为什么我不能在开关/外壳中使用无符号短路?

时间:2021-08-04 21:12:09

I have two static member declarations in ClsA, like this:

我在ClsA中有两个静态成员声明,​​如下所示:

class ClsA {
public:
   static unsigned short m_var1;
   static unsigned short m_var2;
};

unsigned short ClsA::m_var1 = 1001;
unsigned short ClsA::m_var2 = 1002;

In ClsB, I use those static member declarations from ClsA like this:

在ClsB中,我使用ClsA中的那些静态成员声明,​​如下所示:

unsigned short var1; // assume var1 is declare/use some where in the code.

switch( var1 ) {
case ClsA::m_var1:  // Error: cannot appear in a constant-expression
   break;

case ClsB::m_var2:  // Error: cannot appear in a constant-expression
   break;
}

Why do I get an error if I use that in a switch statement? There is no error if I use it in an if statement.

如果我在switch语句中使用它,为什么会出现错误?如果我在if语句中使用它,则没有错误。

5 个解决方案

#1


11  

C++ requires the case to have a constant-expression as its argument. What does that mean? It means that the only operands that are legal in constant expressions are:

C ++要求case具有constant-expression作为其参数。那是什么意思?这意味着在常量表达式中合法的唯一操作数是:

  • Literals
  • Enumeration constants
  • Values declared as const that are initialized with constant expressions
  • 声明为const的值,使用常量表达式初始化

  • sizeof expressions

In your case, if you declared your static members as const, and initialized them when declared with an integral constant expression, you could use them in switch-case statements. For example,

在您的情况下,如果您将静态成员声明为const,并在使用整型常量表达式声明时初始化它们,则可以在switch-case语句中使用它们。例如,

class ClsA {
    public:
        static const unsigned short m_var1 = 13;
        static const unsigned short m_var2 = 42;
};

If, on the other hand, you insist on switching on a variable to avoid multiple if-else if statements, I would suggest using a jump table (it's also referred as a lookup table).

另一方面,如果你坚持打开变量以避免多个if-else if语句,我建议使用跳转表(它也称为查找表)。

#2


10  

Try

static const unsigned short m_var1;

static const unsigned short m_var1;

#3


0  

m_var1 and m_var2 aren't constants. But the cases in switch have to be constant expressions (1, 4*8, some_const+1).

m_var1和m_var2不是常量。但是switch中的情况必须是常量表达式(1,4 * 8,some_const + 1)。

#4


0  

It's because the expressions after the case keyword must be a compile time constants and your m_var1 and m_var2 arent. If need to do this kind of test, use an if chain.

这是因为case关键字后面的表达式必须是编译时常量,而m_var1和m_var2不是。如果需要进行此类测试,请使用if链。

http://gcc.gnu.org/ml/gcc-help/2005-12/msg00069.html talks about this error.

http://gcc.gnu.org/ml/gcc-help/2005-12/msg00069.html谈论此错误。

#5


0  

The values must be compile time constants as the error message indicates. They should be declared and defined as const inside the class declaration, such that the compiler knows about them and their values at any point of compilation, typically in a header file.

值必须是编译时常量,如错误消息所示。它们应该在类声明中声明并定义为const,以便编译器在任何编译点知道它们及其值,通常在头文件中。

class ClsA {
public:
   static unsigned short const m_var1 = 1001;
   static unsigned short const m_var2 = 1002;
};

In some object file you should then also instantiate these const variables

在某些目标文件中,您还应该实例化这些const变量

unsigned short const ClsA::m_var1;
unsigned short const ClsA::m_var2;

that is without repeating the initialization value and without the static keyword.

即没有重复初始化值且没有static关键字。

#1


11  

C++ requires the case to have a constant-expression as its argument. What does that mean? It means that the only operands that are legal in constant expressions are:

C ++要求case具有constant-expression作为其参数。那是什么意思?这意味着在常量表达式中合法的唯一操作数是:

  • Literals
  • Enumeration constants
  • Values declared as const that are initialized with constant expressions
  • 声明为const的值,使用常量表达式初始化

  • sizeof expressions

In your case, if you declared your static members as const, and initialized them when declared with an integral constant expression, you could use them in switch-case statements. For example,

在您的情况下,如果您将静态成员声明为const,并在使用整型常量表达式声明时初始化它们,则可以在switch-case语句中使用它们。例如,

class ClsA {
    public:
        static const unsigned short m_var1 = 13;
        static const unsigned short m_var2 = 42;
};

If, on the other hand, you insist on switching on a variable to avoid multiple if-else if statements, I would suggest using a jump table (it's also referred as a lookup table).

另一方面,如果你坚持打开变量以避免多个if-else if语句,我建议使用跳转表(它也称为查找表)。

#2


10  

Try

static const unsigned short m_var1;

static const unsigned short m_var1;

#3


0  

m_var1 and m_var2 aren't constants. But the cases in switch have to be constant expressions (1, 4*8, some_const+1).

m_var1和m_var2不是常量。但是switch中的情况必须是常量表达式(1,4 * 8,some_const + 1)。

#4


0  

It's because the expressions after the case keyword must be a compile time constants and your m_var1 and m_var2 arent. If need to do this kind of test, use an if chain.

这是因为case关键字后面的表达式必须是编译时常量,而m_var1和m_var2不是。如果需要进行此类测试,请使用if链。

http://gcc.gnu.org/ml/gcc-help/2005-12/msg00069.html talks about this error.

http://gcc.gnu.org/ml/gcc-help/2005-12/msg00069.html谈论此错误。

#5


0  

The values must be compile time constants as the error message indicates. They should be declared and defined as const inside the class declaration, such that the compiler knows about them and their values at any point of compilation, typically in a header file.

值必须是编译时常量,如错误消息所示。它们应该在类声明中声明并定义为const,以便编译器在任何编译点知道它们及其值,通常在头文件中。

class ClsA {
public:
   static unsigned short const m_var1 = 1001;
   static unsigned short const m_var2 = 1002;
};

In some object file you should then also instantiate these const variables

在某些目标文件中,您还应该实例化这些const变量

unsigned short const ClsA::m_var1;
unsigned short const ClsA::m_var2;

that is without repeating the initialization value and without the static keyword.

即没有重复初始化值且没有static关键字。