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

标签 c++ c syntax literals

我不知道 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/

相关文章:

syntax - 在非时钟信号上使用上升沿是不好的做法吗?还有其他选择吗?

c++ - 从自由函数与成员函数返回仅移动类型

c++ - Boost.Bind 返回类型

python - 为什么分号在python中工作?

c - 如何在 BSD make 中强制报错

c - 我怎样才能得到想要的输出?

Javascript初学者语法问题

c++ - const 指针作为参数传递给 constexpr 函数

c++ - 为什么我必须在链接器行的末尾传递库?

c - 有没有办法定义一个将表达式(即 int)转换为字符串文字的宏?