7
votes

C++14 introduced the concept of digit separators into literals, along the lines of 3'141'592'653'589. Now this is a great feature for readable code but I was wondering whether it allowed quotes before the numeric portion of a 0x/0b-type literal. It seems to me that:

unsigned int topThreeBits = 0b'1110'0000;
unsigned int hexNum       = 0x'dead'beef;

is more readable than the one without a leading separator:

unsigned int topThreeBits = 0b1110'0000;
unsigned int hexNum       = 0xdead'beef;

because it clearly delineates the base from the digits.

Since I don't yet have a C++14 compiler, I need confirmation one way or another as to whether it allows this.

I know it doesn't make sense for un-prefixed numbers like '123'456, especially since the parser wouldn't know if it was meant to be a char variable or a numeric literal.

But, for prefixed literals, I can't see there's any confusion as to what the token is meant to be at the point the first ' arrives - the 0x/0b has already dictated it's going to be a numeric literal.

1
I just wondered the same. As the answer says, it can't be done (yet?). So to get symmetrical numbers/nibbles/etc, we have to settle for the next best thing: 0b0'1110'0000 (hmm) and 0x0'dead'beef (omg) - underscore_d
@underscore_d, the leading 0 is actually a brilliant idea, not one I'd thought of. The leading zero has no effect on the value and it still allows for a visually distinct "real number portion". - paxdiablo
Yup, simple but effective, and sometimes such things are easy to miss :-) - underscore_d

1 Answers

8
votes

If we look at the grammar from the draft C++14 standard: N4140 section 2.14.2 [lex.icon], it is not allowed right after the base indicator of hexadecimal or binary literals:

binary-literal:
  0b binary-digit
  0B binary-digit
  binary-literal ’opt binary-digit
[...]
hexadecimal-literal:
  0x hexadecimal-digit
  0X hexadecimal-digit
  hexadecimal-literal ’opt hexadecimal-digit

Although, octal literals do allow the separator after the base indicator:

octal-literal:
  0
  octal-literal ’opt octal-digit

We can also check using one of the online compiler which provide C++14 compilers such as Coliru or Wandbox.

The Evolution Working Group issue which tracked this change was issue 27: N3781 Single-Quotation-Mark as a Digit Separator, N3661, N3499 Digit Separators, N3448 Painless Digit Separation. I don't see an obvious rationale for this design decision, perhaps it is solely a literal interpretation of digit separator.

Note we can find a list of the draft standards from Where do I find the current C or C++ standard documents?.