这是一个比较奇怪的问题...
我正在尝试使用一些解压算法。与其通过循环遍历 char buffer[]
直到找到 buffer[i]
中的停止位,我正在尝试使用一些字符掩码技术。
我有以下示例:
// In a *.h file
const char ch = '\x81';
// To avoid Endianess
union CharUInt
{
char sz[4];
unsigned int u;
};
// Legal because char[] is declared before uint32 in the union
const CharUInt Mask1 = {'\x81', '\x0', '\x0', '\x81'};
const CharUInt Mask2 = {'\x0', '\x81', '\x81', '\x0'};
// Proxy / Auxillary uint32 as usimg Mask2.u in the switch blocked produced the same errors
const unsigned int uMask1 = Mask1.u;
const unsigned int uMask2 = Mask2.u;
const unsigned int uMask_ = (uMask1 & uMask2);
// buf is always long enough
bool Foo(char buf[])
{
const CharUInt Type = {buf[0], buf[1], buf[2], buf[3]};
unsigned int uType = (Type.u & uMask_);
switch(uType)
{
case uMask1:
// do stuff
case uMask2:
// do more stuff
return true;
break;
default:
// do different stuff
return false;
break;
}
};
不考虑“union”语法(实际代码可以编译运行),也不考虑“Foo”函数返回是否漂亮,我得到以下错误:
'uMask1' cannot appear in a constant-expression
如果使用联合本身,则会出现以下错误:
'Mask1' cannot appear in a constant-expression
'.' cannot appear in a constant-expression
当然,这些错误也适用于uMask2和Mask2.u。
我错过了什么?
提前感谢。