MSDN https://msdn.microsoft.com/en-us/library/69ze775t.aspx VS字符字面量描述如下:
Multiple characters in the literal fill corresponding bytes as needed from high->order to low-order. To create a char value, the compiler takes the low-order byte.
根据文档,下面的结果应该是我想要的。
unsigned int i = '1234'; // i = 0x34333231, memory[low->high] [0x31, 0x32, 0x33, 0x34]
然而,当出现转义序列时,情况发生了变化,这是我在我的电脑上所做的结果。
unsigned int i = '1234'; // i = 0x34333231
unsigned int j = '\1\2\3\4\'; // j = 0x01020304 <- ???????
等一下,这里刚刚发生了什么?我很期待变体 j 是 0x04030201。高阶到低阶的东西在哪里?我自己想不通。
这应该是我的第一个问题。八进制转义时,为什么编译器不从高位向低位填充内存?
但这还不是全部,我会在这里展示一些更有趣的东西
unsigned int k = '0\101\1001'; // k = 0x31403041 memory[low->high] [0x41 0x30 0x40 0x31] ??what the hell
unsigned int l = '0\1011\100'; // l = 0x40304131 memory[low->high] [0x31 0x41 0x30 0x40] ??what the hell again
到目前为止,我完全迷路了。我什至无法从这些测试用例中得出简单的规则。
没有人知道这个问题吗?谢谢。
最佳答案
我看不出这里有什么矛盾。当您使用字 rune 字为 int
类型赋值时,它们的处理方式与为 wchar_t
赋值的方式相同。唯一的区别是,int
占用四个字节,而 wchar_t
占用两个字节(通常)。请参阅 documentation page 中的以下示例:
wchar_t w1 = L'\100'; // L'@'
wchar_t w2 = L'\1000'; // C4066 L'@', 0 ignored
wchar_t w3 = L'\009'; // C4066 L'\0', 9 ignored
wchar_t w4 = L'\089'; // C4066 L'\0', 89 ignored
wchar_t w5 = L'\qrs'; // C4129, C4066 L'q' escape, rs ignored
wchar_t w6 = L'\x0050'; // L'P'
wchar_t w7 = L'\x0pqr'; // C4066 L'\0', pqr ignored
关于c++ - VS2013 字 rune 字不符合 MSDN 规范,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36886377/