As of C++14, thanks to n3781 (which in itself does not answer this question) we may write code like the following:
const int x = 1'234; // one thousand two hundred and thirty four
The aim is to improve on code like this:
const int y = 100000000;
and make it more readable.
The underscore (_
) character was already taken in C++11 by user-defined literals, and the comma (,
) has localisation problems — many European countries bafflingly† use this as the decimal separator — and conflicts with the comma operator, though I do wonder what real-world code could possibly have been broken by allowing e.g. 1,234,567
.
Anyway, a better solution would seem to be the space character:
const int z = 1 000 000;
These adjacent numeric literal tokens could be concatenated by the preprocessor just as are string literals:
const char x[5] = "a" "bc" "d";
Instead, we get the apostrophe ('
), not used by any writing system I'm aware of as a digit separator.
Is there a reason that the apostrophe was chosen instead of a simple space?
† It's baffling because all of those languages, within text, maintain the notion of a comma "breaking apart" an otherwise atomic sentence, with a period functioning to "terminate" the sentence — to me, at least, this is quite analogous to a comma "breaking apart" the integral part of a number and a period "terminating" it ready for the fractional input.
1,000,000
and might expect anything other than concatenation of those literals, in reality? The closest I can get isfoo()*3, 4, 5
but I think requiring parens around the first expression is reasonable. Because it's silly code in the first place. – Altisint a[] = {123,000,000}
. As for the comma versus period distinction, note that these are fairly recently standardized - both in text and numbers. – SuppositiveIV.I.MMXV
is today. – Suppositive123.45
delimits integral and fractional); this is a different function than that served by a thousands separator, which is purely aesthetic rather than semantic in use. As such, your would-be counterexample is just another example of why the modern English comma-as-thousands-separator makes sense (over the use of a period for the same thing) and has done since before the 19th century. :) – Altisint foo(int);int foo(int,int); foo(1,000);
– Baudadding machine
it looks more like half/half, not "the vast majority". – Altisa·sin(A)
which is the same asa(sin(A))
, whereasv = (2,3)
is very different fromv = (2(3))
. Anyway, logical recourses to precedent to choose between localisations never really give us the right answer. – AgnateEigen::Matrix3f m; m << 1,2,3,4,5,6,7,8,9;
. See Eigen's comma initializer. – OlavEigen
type on the LHS) and could be defined to take precedence over a "single" literal found in a subexpression on its own. Logically the two could be distinguished but, admittedly, it's otherwise ambiguous and the parsing stage may not want to have to work that out. – Altis