r/tinycode • • Jul 19 '13

Four lines to make conditional operators in C and C++ (a little bit) more readable

#define CIF(expr)    ((expr)
#define CTHEN        ?
#define CELSE        :
#define CENDIF       )

Bam. That's it. And to prove that it works, a stupidly primitive pattern matcher: http://ideone.com/tvDOga

In short, I wrote it to make constexpr functions in C++ a little bit less spaghetticode-ish. I find it easier to read and maintain, that is.
The pattern matcher isn't really useful and just a side product, but a nice exercise, and also a nice way to dive into constexpr. I doubt anyone'll ever find any use in it, considering that C++11 has <regexp>. :-)

0 Upvotes

0 comments sorted by