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