十六进制或二进制数字中的数字之前是否允许使用数字分隔符? [英] Are digit separators allowed before the digits in a hex or binary number?
问题描述
C ++ 14沿 3’141’592’653’589
的概念将数字分隔符的概念引入了文字。现在,这是可读代码的伟大功能,但我想知道它是否允许在 0x / 0b $ c的数字部分之前引号。 $ c>类型的文字。在我看来:
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.
由于我还没有C ++ 14编译器,因此我需要一种或另一种方式确认它是否允许这样做。
Since I don't yet have a C++14 compiler, I need confirmation one way or another as to whether it allows this.
我知道对于像'123'456
这样的非前缀数字没有意义,尤其是因为解析器不知道它是否应该是 char
变量或数字文字。
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.
但是,对于带前缀的文字,我看不到令牌的含义有任何混淆在第一个'
到达的时刻- 0x / 0b
已经规定它将是一个数字文字
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.
推荐答案
如果我们从草稿C ++ 14标准:N4140 部分 2.14.2
[lex.icon] ,不允许在十六进制的基本指示符之后或二进制文字:
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
我们还可以使用提供C ++ 14编译器的在线编译器之一进行检查,例如 Coliru 或 Wandbox 。
We can also check using one of the online compiler which provide C++14 compilers such as Coliru or Wandbox.
跟踪此更改的Evolution Working Group问题是问题27:N3781单引号标记为数字分隔符,N3661,N3499数字分隔符,N3448无痛数字分隔。我认为此设计决定没有明显的理由,也许它只是数字分隔符的字面解释。
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.
请注意,我们可以找到在哪里可以找到当前的C或C ++标准文档?。
Note we can find a list of the draft standards from Where do I find the current C or C++ standard documents?.
这篇关于十六进制或二进制数字中的数字之前是否允许使用数字分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!