引用 22 楼 frankhb1989 的回复:
21L是ISO C++03的说法。关于C++0x,至少“An ordinary character literal that contains more than
one c-char is a multicharacter literal. A multicharacter literal has type int and implemen……
[/Quote]
implementation-defined 不是 undefined。
这里是 C++ 版,肯 C 标准有啥用。
char p = 'abc'; 也不是语法错误,绝大多数实现只是数据截断。
[Quote=引用 22 楼 frankhb1989 的回复:]
21L是ISO C++03的说法。关于C++0x,至少“An ordinary character literal that contains more than
one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.”在N3242的说辞不变。
因为并非undefined behavior,也不违反三大正确性规则(语法规则、可诊断语义规则、ODR),所以至少从语言角度上来讲不能断言这种用法是错误的。
[/Quote]
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."
依赖实现的定义的就是标准未定义的行为,因为具体处理方式C标准没有规定。
此外像:
char p = 'abc';
这样明显是语法错误。
/不是转义符,这种写法代表一个'/'(0x2f)和一个数字'0'(0x30),这种写法是错误的,不过有的编译器可能为了兼顾wchar_t,允许你这么写。因为Intel的变量是低字节在前,所以内存中是0x30 0x2f这样的顺序,你打印%c的话,就只处理的一个字符。这种非法形式……
[/Quote]
21L是ISO C++03的说法。关于C++0x,至少“An ordinary character literal that contains more than
one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.”在N3242的说辞不变。
因为并非undefined behavior,也不违反三大正确性规则(语法规则、可诊断语义规则、ODR),所以至少从语言角度上来讲不能断言这种用法是错误的。
2.13.2 Character literals [lex.ccon]
character-literal:
’c-char-sequence’
L’c-char-sequence’
c-char-sequence:
c-char
c-char-sequence c-char
c-char:
any member of the source character set except
the single-quote ’, backslash \, or new-line character
escape-sequence
universal-character-name
escape-sequence:
simple-escape-sequence
octal-escape-sequence
hexadecimal-escape-sequence
simple-escape-sequence: one of
\’ \" \? \\
\a \b \f \n \r \t \v
octal-escape-sequence:
\ octal-digit
\ octal-digit octal-digit
\ octal-digit octal-digit octal-digit
hexadecimal-escape-sequence:
\x hexadecimal-digit
hexadecimal-escape-sequence hexadecimal-digit
1 A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by the letter L, as in L’x’. A character literal that does not begin with L is an ordinary character literal, also referred to as a narrow-character literal. An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set. An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.
...