Multi line preprocessor macros

C++CC Preprocessor

C++ Problem Overview


How to make multi line preprocessor macro? I know how to make one line:

#define sqr(X) (X*X)

but I need something like this:

#define someMacro(X)
    class X : public otherClass
    {
         int foo;
         void doFoo();
    };

How can I get this to work?

This is only an example, the real macro may be very long.

C++ Solutions


Solution 1 - C++

You use \ as a line continuation escape character.

#define swap(a, b) {               \
                       (a) ^= (b); \
                       (b) ^= (a); \
                       (a) ^= (b); \
                   }

EDIT: As @abelenky pointed out in the comments, the \ character must be the last character on the line. If it is not (even if it is just white space afterward) you will get confusing error messages on each line after it.

Solution 2 - C++

You can make a macro span multiple lines by putting a backslash (\) at the end of each line:

#define F(x) (x)   \
              *    \
             (x)
              

Solution 3 - C++

PLEASE NOTE as Kerrek SB and coaddict pointed out, which should have been pointed out in the accepted answer, ALWAYS place braces around your arguments. The sqr example is the simple example taught in CompSci courses.

Here's the problem: If you define it the way you did what happens when you say "sqr(1+5)"? You get "1+51+5" or 11
If you correctly place braces around it, #define sqr(x) ((x)
(x))
you get ((1+5) * (1+5)) or what we wanted 36 ...beautiful.

Ed S. is going to have the same problem with 'swap'

Solution 4 - C++

You need to escape the newline at the end of the line by escaping it with a \:

#define sqr(X) \
        ((X)*(X))

Solution 5 - C++

Although this wasn't part of the original question, none of the other answers mention that comments embedded in multi-line macros require careful attention.

  • C++ style comments may not appear on any line having a line continuation escape character.
  • C-style comments may not span multiple lines separated by a line continuation escape character.

Examples:

// WRONG:
#define someMacro(X)          \
// This comment is a problem. \
class X : public otherClass   \
{                             \
     int foo;                 \
     void doFoo();            \
};

// WRONG:
#define someMacro(X)        \
/* This comment is also     \
 * a problem. */            \
class X : public otherClass \
{                           \
     int foo;               \
     void doFoo();          \
};

// OK:
#define someMacro(X)                \
/* This comment is fine. */         \
class X : public otherClass         \
{                                   \
     int foo; /* This is OK too! */ \
     void doFoo();                  \
};

Solution 6 - C++

This is 2021 and we should really be moving towards inline. Incorrect, unnecessary and overuse of macros bloats the code and they are hard to debug ( read really hard to debug )

inline void foo(x)
{
   // do whatever with x
}

if, however macros are really the need of the hour, surrounding them with a do { } while(0); reason is explained in this post Why use apparently meaningless do-while and if-else statements in macros?

Solution 7 - C++

We can write multi-line macro same like function, but each statement ends with “\”. Let us see with example. Below is simple macro, which accepts input number from user, and prints whether entered number is even or odd

#include <stdio.h>
  
#define MACRO(num, str) ({\
            printf("%d", num);\
            printf(" is");\
            printf(" %s number", str);\
            printf("\n");\
           })
  
int main(void)
{
    int num;
  
    printf("Enter a number: ");
    scanf("%d", &num);
  
    if (num & 1)
        MACRO(num, "Odd");
    else
        MACRO(num, "Even");
  
    return 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnoisy catView Question on Stackoverflow
Solution 1 - C++Ed S.View Answer on Stackoverflow
Solution 2 - C++Kerrek SBView Answer on Stackoverflow
Solution 3 - C++jiveturkeyView Answer on Stackoverflow
Solution 4 - C++codaddictView Answer on Stackoverflow
Solution 5 - C++siffermanView Answer on Stackoverflow
Solution 6 - C++asio_guyView Answer on Stackoverflow
Solution 7 - C++Abhishek KaushikView Answer on Stackoverflow