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

你说的曾经没有我的故事 提交于 2019-12-23 08:54:43

问题


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:


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



来源:https://stackoverflow.com/questions/32556762/are-digit-separators-allowed-before-the-digits-in-a-hex-or-binary-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!