René Richter René Richter - 1 month ago 15
C++ Question

Are C++14 digit separators allowed in user defined literals?

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

auto time = 01'23s;


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

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.

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




(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.)

Answer

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 ’opt octal-digit.

N4140 §2.14.8

user-defined-literal:

  • user-defined-integer-literal
  • [...]

user-defined-integer-literal:

  • octal-literal ud-suffix
  • [...]

N4140 §2.14.2

octal-literal:

  • 0
  • octal-literal ’opt octal-digit

So 01'23s is a perfectly valid literal.