Ruby-style digit separator for C-98?

A silly, and not very useful tip: I love how ruby allows you to write numbers like 1000000 as 1_000_000. Very useful to write benchmarking tests.

Until we get to C++14 we don’t have a nice alternative, but we have an ugly hack we can use: instead of writing 1000000 write “1 ## 000 ## 000”.

It works, ‘##’ is the preprocessor’s token pasting operator, and it will paste two tokens together. Looks ugly, breaks the GUI highlighting, but at least you can count how many zeros you’ve got.

Nitpicker’s corner: multiplying by 10 is easier, but there is no job-safety involved in that.

Nitpicker’s corner II: The evaluation order of a chain of ‘##’ is not defined, but I don’t expect this to cause any problems; any order of evaluation should result in the same result for this case.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s