8

What guarantees does C++ make about the ordering of character literals? Is there a definite ordering of characters in the basic source character set? (e.g. is 'a' < 'z' guaranteed to be true? How about 'A' < 'z'?)

Luke
  • 7,110
  • 6
  • 45
  • 74
  • A really evil implementation could have `'z' == 'a' + 25` and _still_ have `'b'<'a'`. I believe this is rather underspecified, even accounting for EBCDIC and SMS. – MSalters Sep 15 '15 at 13:19
  • 1
    The fact that there is no guarantee shows again that it is sometimes a good idea to stick your support to some implementations, which can be "most". A good example in my opinion is Qt sticking to implementations which has `sizeof(int) == 4`... All compilers they want to support guarantee this, but of course not all, but if you limit yourself to such implementations, it can make life much easier. However you need to be careful and know the implementations you're sticking to, as well as consider such a limitation dependent on the application, of course. – leemes Sep 15 '15 at 13:37

1 Answers1

12

The standard only provides a guarantee for ordering of the decimal digits 0 to 9, from the draft C++11 standard section 2.3 [lex.charset]:

In both the source and execution basic character sets, the value of each character after 0 in the above list of decimal digits shall be one greater than the value of the previous.

and otherwise says (emphasis mine):

The basic execution character set and the basic execution wide-character set shall each contain all the members of the basic source character set, plus control characters representing alert, backspace, and carriage return, plus a null character (respectively, null wide character), whose representation has all zero bits. For each basic execution character set, the values of the members shall be non-negative and distinct from one another.

Note, EBCDIC has a non-consecutive character set.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740