2014-11-22 CPlusPlus11

Digit Separators

When I talked about the binary literals recently I also mentioned digit separators. I thought it was part of C++0x but apparently it is also a new C++1y feature that I have not mentioned yet. Sorry for that. There are so many features so I'm getting confused. ;-) Time to change that then.

Anyway, digit separators is a way to write big numbers so they are easier to read for humans. The apostrophe character is what is used to group or separate digits in a number. It can look like this:

   int decimal = 4'096;
   int binary = 0b0001'0000'0000'0000;
   int hexadecimal = 0x10'00;
   int octal = 0'10'000;
   double x = 1.234'567'890e-19;

Look how the apostrophe neatly groups the digits in the way we are use to see them.

The position of the apostrophe can however be anywhere in the number. It does not have to be according to the radix or any other normal way numbers are written out there in real life. It can be used in any way you like. The later is probably a bad idea but, who knows, there might be cases where it can be put to good use.