A walkthrough of the 'macro overloading' idiom in C/C++, which lets a single macro dispatch to different implementations based on the number of arguments. Using variadic macros and a helper GET_*_MACRO pattern, you can make M(x) and M(x,y) call different functions. Examples include ARCTAN dispatching to atan vs atan2, and HYPOT handling 1–3 arguments. The post also covers edge cases (zero args, too many args), the C23/C++20 __VA_OPT__ improvement, and an MSVC workaround using an extra EXPAND indirection layer.
Sort: