C++ 多字符常量警告
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7755202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multi-character constant warnings
提问by Mircea Ispas
Why is this a warning? I think there are many cases when is more clear to use multi-char int constants instead of "no meaning" numbers or instead of defining const variables with same value. When parsing wave/tiff/other file types is more clear to compare the read values with some 'EVAW', 'data', etc instead of their corresponding values.
为什么这是一个警告?我认为在很多情况下,使用 multi-char int 常量而不是“无意义”数字或定义具有相同值的 const 变量更清楚。当解析 wave/tiff/其他文件类型时,将读取的值与某些“EVAW”、“数据”等而不是它们的相应值进行比较会更加清晰。
Sample code:
示例代码:
int waveHeader = 'EVAW';
Why does this give a warning?
为什么这会发出警告?
采纳答案by Mircea Ispas
According to the standard(§6.4.4.4/10)
根据标准(§6.4.4.4/10)
The value of an integer character constant containing more than one character (e.g., 'ab'), [...] is implementation-defined.
包含多个字符(例如,'ab')[...] 的整数字符常量的值是实现定义的。
long x = '\xde\xad\xbe\xef'; // yes, single quotes
This is valid ISO 9899:2011 C. It compiles without warning under gcc
with -Wall
, and a “multi-character character constant” warning with -pedantic
.
这是有效的ISO 9899:2011三它编译没有警告的情况下gcc
用-Wall
,和“多字符常量”与警告-pedantic
。
From Wikipedia:
来自维基百科:
Multi-character constants (e.g. 'xy') are valid, although rarely useful — they let one store several characters in an integer (e.g. 4 ASCII characters can fit in a 32-bit integer, 8 in a 64-bit one). Since the order in which the characters are packed into one int is not specified, portable use of multi-character constants is difficult.
多字符常量(例如'xy')是有效的,尽管很少有用——它们让一个整数存储多个字符(例如,4 个 ASCII 字符可以存储在 32 位整数中,8 个可以存储在 64 位整数中)。由于未指定字符打包成一个 int 的顺序,因此难以移植使用多字符常量。
For portability sake, don't use multi-character constants with integral types.
为了可移植性,不要使用具有整数类型的多字符常量。
回答by Didier Trosset
This warning is useful for programmers that would mistakenly write 'test'
where they should have written "test"
.
此警告对于那些错误地将其写'test'
在他们应该写的地方的程序员很有用"test"
。
This happen much more often than programmers that do actually want multi-char int constants.
这比真正想要多字符 int 常量的程序员更常发生。
回答by blueshift
If you're happy you know what you're doing and can accept the portability problems, on GCC for example you can disable the warning on the command line:
如果您很高兴知道自己在做什么并且可以接受可移植性问题,例如在 GCC 上,您可以在命令行上禁用警告:
-Wno-multichar
I use this for my own apps to work with AVI and MP4 file headers for similar reasons to you.
出于与您类似的原因,我将它用于我自己的应用程序以处理 AVI 和 MP4 文件头。
回答by o11c
Even if you're willing to look up what behavior your implementation defines, multi-character constants will stillvary with endianness.
即使您愿意查找您的实现定义的行为,多字符常量仍会随字节顺序而变化。
Better to use a (POD) struct { char[4] }; ... and then use a UDL like "WAVE"_4cc to easily construct instances of that class
最好使用 (POD) struct { char[4] }; ...然后使用像 "WAVE"_4cc 这样的 UDL 来轻松构建该类的实例
回答by sqr163
Simplest C/C++ any compiler/standard compliant solution, was mentioned by @leftaroundabout in comments above:
@leftaroundabout 在上面的评论中提到了最简单的 C/C++ 任何编译器/标准兼容解决方案:
int x = *(int*)"abcd";
Or a bit more specific:
或者更具体一点:
int x = *(int32_t*)"abcd";
One more solution, also compliant with C/C++ compiler/standard since C99 (except clang++, which has a known bug):
另一种解决方案,也符合 C99 以来的 C/C++ 编译器/标准(除了 clang++,它有一个已知的错误):
int x = ((union {char s[5]; int number;}){"abcd"}).number;
/* just a demo check: */
printf("x=%d stored %s byte first\n", x, x==0x61626364 ? "MSB":"LSB");
Here anonymous union is used to give a nice symbol-name to the desired numeric result, "abcd" string is used to initialize the lvalue of compound literal(C99).
这里匿名联合用于为所需的数字结果提供一个很好的符号名称,“abcd”字符串用于初始化复合文字(C99)的左值。
回答by Alexander Ushakov
If you want to disable this warning it is important to know that there are two related warning parameters in GCC and Clang: GCC Compiler options -wno-four-char-constants and -wno-multichar
如果要禁用此警告,重要的是要知道 GCC 和 Clang 中有两个相关的警告参数:GCC 编译器选项 -wno-four-char-constants 和 -wno-multichar