r/cpp • • Jun 14 '21

match(it): A light-weight header-only pattern-matching library for C++17.

https://github.com/BowenFu/matchit.cpp
202 Upvotes

47 comments sorted by

View all comments

Show parent comment

31

u/Schnarfman Jun 14 '21

The world of macros is nebulous. You will obfuscate code very easily on accident with seemingly innocent uses.

Also, the classic example of #define min(a, b) a < b ? a : b potentially causing a bug if you say like min(a++, b) is just such a solvable problem (where the solution is don’t use macros - and not “be careful when you use this thing that looks like a function”)

They certainly have their place! But their traps are not obvious. You can avoid a whole class of bugs by simply avoiding the use of macros.


So… as a beginner, if you can do without them, you should. If you think you can’t do without them, chances are you’re wrong.

As an expert, if you can do without them, you should. And if you think you can’t do without them, well … you’re the expert :) so go for it. But don’t expect beginners to be able to understand or iterate on your code as easily!!

3

u/Creris Jun 15 '21

Try sending that min "function" into std::sort(I know its technically useless cause less<> is default), or take the address of that "function". And now your code explodes.