c++ - C 和 C++ 中的多字 rune 字

我不知道 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 中被广泛使用.但是当我们谈论平台无关的序列化时,我认为它们并不安全。 Thay 也可能令人困惑,因为它看起来像字符串。那么多字 rune 字的使用范围是什么,它们对某些东西有用吗?它们在 C++ 中只是为了与 C 代码兼容吗?作为 goto 运算符,它们是否被认为是一个不好的功能?

最佳答案

这使得在内存转储中挑选值变得更加容易。

例子:

enum state { waiting, running, stopped };

对比

enum state { waiting = 'wait', running = 'run.', stopped = 'stop' };

以下语句后的内存转储:

s = stopped;

可能看起来像:

00 00 00 02 . . . .

在第一种情况下,vs:

73 74 6F 70 s t o p

使用多字 rune 字。 (当然是说“stop”还是“pots”取决于字节顺序)

https://stackoverflow.com/questions/3960954/

相关文章:

c++ - 为什么 unique_ptr 实例化编译为比原始指针更大的二进制文件?

c++ - 为什么这个包含 rand() 的 C++11 代码多线程比单线程慢?

c++ - 访问类中定义的 friend 功能

c++ - 在 C++ 中设置本地环境变量

c++ - 为 Windows 编写超薄 C++ 程序(如 uTorrent)

c++ - 标准的 atomic bool 和 atomic flag 之间的区别

c++ - QObject 多重继承

c++ - 向 Python 公开 C++ API

c++ - 架构 x86_64 : Compiling problems 的 undefined s

c++ - 为什么我们在 C++ 中使用 std::function 而不是原来的 C 函数指针?