Here is the statement of the problem as described by Lawrence Crowl
- Decide whether
The paper then goes on to describe a proposal the C++ standard to add digit separators to C++. In this blog post, we will look at what we can do for this problem in C++ as it is currently.
Doing this involves (ab)using the preprocessor ## operator which concatenates 2 tokens. Using this yields two different options. We will use the number 1234567 as an example for these 2 options.
Option 1 – Self-contained but not terse
This option has the advantage that anyone that knows C would be able to figure out what is going on without looking at any other code. The disadvantage is that this is definitely not terse.
Option 2 – Terse but requires macro
Given the macro below
We can write the example number 1234567 as below
The advantage is that this option is terse. It also has the same format that is commonly used outside of programming. The disadvantage is that the code looks pretty confusing unless you know what NUM does. The other disadvantage is that the macro also pollutes the namespace.
While both of these approaches are inferior to C++ native support for digit separators, the two options described above work with C++ now.
Please let me know what you think and which option you like better.
- John Bandela