Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are digit separators allowed before the digits in a hex or binary number?

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.

like image 964
paxdiablo Avatar asked Sep 14 '15 03:09

paxdiablo


Video Answer


1 Answers

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

like image 87
Shafik Yaghmour Avatar answered Sep 27 '22 22:09

Shafik Yaghmour