37

I'm writing a few large macros and thought it'd be nice if I could add comments to them, like this:

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL() \ // this does...
  SOME_OTHER_FUNCTION_CALL()

When I run this I get:

prog.cpp:9:2: error: stray ‘\’ in program

Is there any way around this, or is it just not possible to comment mult-line macros?

4
  • 1
    As far I know the macro line separator (`\`) expects to be the last character of a line, for example, if you put spaces after the backslash the preprocessor complains.
    – Manu343726
    Jul 15, 2014 at 6:36
  • 5
    I think it works if you use /* C style comments */ ?
    – Paul R
    Jul 15, 2014 at 6:37
  • Please try SOME_FUNCTION_CALL() \// this does... without a space. Does it work now? Jul 15, 2014 at 6:37
  • @AlexanderGelbukh no that doesn't work.
    – quant
    Jul 15, 2014 at 6:56

5 Answers 5

49

There is no way to use // comments in a macro except the last line of the macro.

As Paul R suggests, the /* comment */ does work and seems to be the only option:

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL()  /* this does... */ \
  SOME_OTHER_FUNCTION_CALL()

The reason is the following. Standard for Programming Language C ++ (I only have access to this draft) specifies that physical lines of the source file can be concatenated into logical lines that the compiler will see by using \ followed by a newline:

Each instance of a backslash character () immediately followed by a new-line character is deleted, splicing physical source lines to form logical source lines. Only the last backslash on any physical source line shall be eligible for being part of such a splice.

This can be easily checked in the preprocessor output: create file.cpp with

pri\
ntf ("Hell\
o world"\
);

then

cpp file.cpp

gives

printf ("Hello world");

or

printf 
    ("Hello world");

which is what the compiler sees (checked on Ubuntu; your mileage can vary).

Now, applying this rule to a multiline macro,

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL() \
  SOME_OTHER_FUNCTION_CALL()

is understood by the preprocessor as

#define SOME_BIG_MACRO(input)   SOME_FUNCTION_CALL()   SOME_OTHER_FUNCTION_CALL()

because all \ and the next newline are ignored.

Similarly,

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL()  /* this does... */ \
  SOME_OTHER_FUNCTION_CALL()

is seen by the preprocessor as

#define SOME_BIG_MACRO(input)   SOME_FUNCTION_CALL()  /* this does... */   SOME_OTHER_FUNCTION_CALL()

However,

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL()  \ // this does...
  SOME_OTHER_FUNCTION_CALL()

becomes two lines:

#define SOME_BIG_MACRO(input)   SOME_FUNCTION_CALL()  \ // this does...
  SOME_OTHER_FUNCTION_CALL()

because the second \ is not followed by newline and thus is preserved, as well as a newline not preceeded by a \. This causes compile error.

While

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL()  // this does... \
  SOME_OTHER_FUNCTION_CALL()

becomes one line:

#define SOME_BIG_MACRO(input)  SOME_FUNCTION_CALL()  // this does...   SOME_OTHER_FUNCTION_CALL()

which is syntactically correct but the macro is incomplete. Some compilers report this as an error, because most probably this is not the intention of the programmer. Others, such as Ubuntu cc, silently apply the rules defined by the standard.

Since a macro can occupy only one logical line (though several physical lines, using the newline escaping mechanism), any // comment on this line causes all the rest of the macro to be ignored.

Conclusion: a // comment can only occur at the end of a (multi- or single-line) macro, while /* comment */ can perfectly be used inside the macro.

2
  • 1
    A reference to the standard seems proper for this answer
    – Martin G
    Jul 15, 2014 at 7:18
  • 3
    Done. As requested, I modified the answer adding a reference to the standard and a detailed explanation of it. Jul 15, 2014 at 9:30
7

Line comment // won't do, only block comment /* ... */ e.g.

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL() /* this does... */ \ 
  SOME_OTHER_FUNCTION_CALL()
3

According to this answer:

https://stackoverflow.com/a/11722479/3545094

Comments are replaced by one space character before expanding the macro during the pre-processing.

\ escapes ONE character, which has to be \n for the macro to work, as explained by previous answers/comments.

This means that the comment has to be located before \ in the macro, hence // will not work since you would then remove the \ which is needed for the macro to work.

1
  • 2
    The explanation that "the comment has to be located before \" is incorrect: the comment does not prevent \<newline> from being deleted and the next line from being glued with the current line and thus ignored (since you are talking of // comment). The difference is easily seen: if the // comment prevented the \<newline> from being interpreted in this way, then the next line would be seen by the compiler and caused a compile error, which does not happen. Jul 15, 2014 at 9:44
2

May be something like

#define MY_MACRO(a, b, c) \
static_assert(true, "My comments go here..."); \
...
1
  • IMO, this is the best answer because there are reasons to avoid block comments, primarily because they don’t nest. static_assert(true) won’t trigger any profilers etc., in contrast to e.g. a string literal.
    – Bolpat
    Apr 28 at 14:40
1

I’ll assume a block comment does not do the trick for you – for whatever reason. As a quick and dirty solution, you could just put in a string literal that isn’t used:

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL(), (void)"this does..." \
  SOME_OTHER_FUNCTION_CALL()

It’s not great and could trigger style checkers (esp. without the void cast). A static_assert is strictly better (cf. @Zinovy Nis’s answer); but if you happen to need to support C++03 or C++98, no static_assert for you.

As you’re already using macros, a better alternative could be to define a COMMENT macro that just ignores anything that goes into it and has the advantage to rather clearly document intent:

#define COMMENT(x)
#define COMMENT(...) // C++11 and onward: No need for quotes!

In your code:

#define SOME_BIG_MACRO(input)\
  SOME_FUNCTION_CALL() COMMENT("this does...") \
  SOME_OTHER_FUNCTION_CALL()

Just to be clear: I do not recommend any of this, I’m pointing out that these are working solutions that “get the job done” that OP asked for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.