8

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 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](https://sites.google.com/site/ohmusicstudent/_/rsrc/1315398151002/band-instruments/oboe/oboe-history/oboe2.gif)) and `0x0'dead'beef` ([omg](http://londonpride.com/wp-content/uploads/2013/09/Oxo__Beef________4c23b399c6687.jpg)) – underscore_d Jul 20 '16 at 07:19
  • @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 Jul 21 '16 at 09:24
  • Yup, simple but effective, and sometimes such things are easy to miss :-) – underscore_d Jul 21 '16 at 09:51

1 Answers1

9

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?.

Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Thanks for that. Nice links too, I've been using codingground but that only appears to go to gcc 4.8.x which is not C++14. – paxdiablo Sep 14 '15 at 03:23
  • Except for the octal base indicator. It is allowed immediately after the `0`. –  Sep 14 '15 at 06:17
  • @hvd I should have been more specified, I updated my answer. – Shafik Yaghmour Sep 14 '15 at 09:37
  • An interesting question is whether the lexical grammar could be extended to allow it. I have to admit I like the look. It is especially helpful for octal (though that works). IIRC when I implemented digit seps in g++ it looked like I could allow it without breaking things. – emsr Sep 14 '15 at 16:34
  • @emsr it looks like it could be supported without a problem, I have not thought too deeply about it. None of the four proposals linked from the EWG issue give a rationale for that decision. I wonder if perhaps it was just thought of a literally as a *digit sperator*. – Shafik Yaghmour Sep 14 '15 at 16:38
  • @ShafikYaghmour I remember having to keep the words _digit separator_ in my mind as I was building and testing. I think that's it. I remember having to special case the bit right after 0[xXbB]. I wonder if it's worth a little paper. – emsr Sep 14 '15 at 16:45
  • @emsr it seems like a reasonable proposal, let me know if ever turns into a proposal. – Shafik Yaghmour Sep 18 '15 at 18:23