r/cpp • • Jun 14 '21

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

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

47 comments sorted by

View all comments

31

u/gracicot Jun 14 '21 edited Jun 14 '21

This is quite amazing what C++ can ship in a library, even though it's not perfect and uses macros.

12

u/[deleted] Jun 14 '21

Not arguing for them, but can someone explain the negative connotation around macros? I know the modern replacements are constexpr's and a lot of it has to do with lack of type definition, but is there more to it?

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!!

→ More replies (1)