本文介绍了用户定义的文字中是否允许使用C ++ 14位分隔符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然clang编译了以下行,但g ++ 6.1抱怨数字分隔符(请参阅Coliru上的实时示例) :

While clang compiles the following line, g++ 6.1 complains about the digit separator (see live example on Coliru):

auto time = 01'23s;

哪个编译器(如果有)根据C ++ 14标准(N3796)是正确的?

Which compiler, if any, is correct according to the C++14 standard (N3796)?

否则,是否允许数字分隔符(第2.14.2节)只是<chrono>库(第20.12.5.8节)的用户定义文字(第2.14.8节)中的实现细节?恕我直言,不应该这样,因为这些文字是在unsigned long long参数上定义的.

Otherwise, is allowing digit separators (§2.14.2) just an implementation detail in the user-defined literals (§2.14.8) of the <chrono> library (§20.12.5.8)? IMHO it should be not, since these literals are defined on unsigned long long parameters.

我记得Howard Hinnant在 CppCon 2016演讲中以10'000s为例<chrono>教程" (在他的演讲中大约有42分钟的时间.)

I remember Howard Hinnant using 10'000s as an example during his CppCon 2016 talk "A <chrono> tutorial" (at about 42 minutes in his talk).

(请注意,我不打算编写"1分23秒"的代码,这只是偶然地纠正,因为八进制字面0123是64 + 16 + 3 == 83为此,我应该写

(Please note, that I did not intend to code "1 minute and 23 seconds", which is only correct by accident, since the octal literal 0123 is 64 + 16 + 3 == 83. For that purpose I should write

auto time = 1min + 23s;

但是可能的误导性解释不是问题的一部分.)

but that possible misleading interpretation is not part of the question.)

推荐答案

如果您查看语法,则 user-defined-integer-literal 可以是 octal-literal ud-suffix ,而八进制被定义为0八进制'八进制.

If you look at the grammar, user-defined-integer-literal can be octal-literal ud-suffix, and octal-literal is defined as either 0 or octal-literal ’ octal-digit.

用户定义的文字:

  • 用户定义的整数文字
  • [...]
  • user-defined-integer-literal
  • [...]

用户定义的整数文字:

  • 八进制文字ud后缀
  • [...]
  • octal-literal ud-suffix
  • [...]

八进制:

  • 0
  • 八进制文字‘八进制数字
  • 0
  • octal-literal ’ octal-digit

所以01'23s是完全有效的文字.

So 01'23s is a perfectly valid literal.

这篇关于用户定义的文字中是否允许使用C ++ 14位分隔符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 04:13