我不知道 C 和 C++ 允许 multicharacter literal
:不是“c”(C 中的 int 类型和 C++ 中的 char 类型),而是“tralivali”(int 类型!)
enum
{
ActionLeft = 'left',
ActionRight = 'right',
ActionForward = 'forward',
ActionBackward = 'backward'
};
标准说:
C99 6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."
我发现它们广泛用于 C4 engine 。但我认为当我们谈论平台无关的序列化时它们并不安全。这也可能令人困惑,因为它看起来像字符串。那么多字 rune 字的使用范围是什么,它们有什么用处吗?它们在 C++ 中只是为了与 C 代码兼容吗?作为 goto 运算符,它们是否被认为是一个不好的功能?
最佳答案
它可以更轻松地在内存转储中挑选出值。
示例:
enum state { waiting, running, stopped };
对比
enum state { waiting = 'wait', running = 'run.', stopped = 'stop' };
以下语句后的内存转储:
s = stopped;
可能看起来像:
00 00 00 02 . . . .
在第一种情况下,对比:
73 74 6F 70 s t o p
使用多字 rune 字。 (当然,它是说“stop”还是“pots”取决于字节顺序)
关于c++ - C 和 C++ 中的多字 rune 字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24424798/