1. 3
  1.  

  2. 1

    Although it says C++, these are good examples outside of C++. They show C’s weaknesses an alternative would want to improve on. They also show use-cases for metaprogramming. A larger survey would be interesting.

    1. 1

      I found the examples very basic or, at times, showing how code is much more verbose when not using macros.

      However, I would find the opposite way more interesting: cases where only macros (or the preprocessor) solve the problem.

      Actually, the article shows two cases where the preprocessor is necessary: for _DEBUG and __FUNCTION__.

      But I have some cases, in both C and C++, where only (as far as I know) the preprocessor solves my problem. For example, a “function” that returns the caller:

      #define FOO(predicate, return_value) \
        do {                               \
          if (predicate) {                 \
            return return_value;           \
          }                                \
        } while(false)
      
      int bar(int parameter) {
        FOO(parameter > 10, parameter - 1);
        FOO(parameter < 10, parameter + 1);
        return parameter;
      }
      

      I usually use this kind of construction to emulate a “monadic error handling” in complex (or legacy) code.

      Of course the preprocessor is a double-edged sword and there are “more modern and safer weapons”. But I miss the discussion on the cases it is really useful and also the reasons it was originally invented.

      Macros is so widespread in the UNIX ecosystem that one must wonder when it is useful.