1

C++ Poll Answer for April 12th 2019

 2 years ago
source link: https://shafik.github.io/poll%20answers/2019/04/12/multicharacter_literals_and_invalid_escapes_answer.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
C++ Poll Answer for April 12th 2019

The answer to this is C, it is conditionally supported. A compiler could make this ill-formed or well-defined.

Looking at [lex.ccon]p2:

… An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal, or an ordinary character literal containing a single c-char not representable in the execution character set, is conditionally-supported, has type int, and has an implementation-defined value.

tells us multicharacter literals have implementation defined values. But we also have to look at [lex.ccon]p7:

…Escape sequences in which the character following the backslash is not listed in Table 8 are conditionally-supported, with implementation-defined semantics…

which tells us that escape sequences not mentioned in the table are conditionally supported.

So while

\77

Is a valid octal escape sequence and has a value of 63

\97

Is not a valid escape sequence it looks like clang and gcc just ignore the \ and treat it like '97' and then we end up with a implementation defined multicharacter literal value. It looks like clang generates a value of 14647 which does not fit in a char and is truncated to a value of 55 So we end up with 55 < 63 Which in this case is true but is not portable.

The gcc manual describes how gcc calculates the value of multicharacter literal.

© Shafik Yaghmour - https://github.com/shafik - Powered by Jekyll.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK