Logo

Blog


Control your numbers in C++

Today's post is probably easier and shorter than usual. But that doesn't mean the feature is any less powerful.

I like to present you C++14's digit operator. Sometimes it is the little things that matter a lot:

1
2
3
4
5
char buffer[2'048]{};

const auto someConstant = 2'00'00'00;

const auto anotherConstant = 0xFF'256'AF;

The single quotes have absolutely no meaning for the compiler. It simply ignores them and treats the digit as a single row of digits. On the other hand, we humans can group digits and increase readability by doing so.

You are absolutely free in how you create your groups. There has to be no balance or scheme. Any scheme is good if the separation helps you and your colleagues.

Take control over your numbers and keep them readable as you would do on paper. I know different spoken languages have different notations. The single quote is not the right separator in all languages, but I hope the C++ notation works for you.

Andreas