Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I comment multi-line macros?

Tags:

c++

macros

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?

like image 680
quant Avatar asked Jul 15 '14 06:07

quant


People also ask

Can multi-line macros be used?

We can write multiline macros like functions, but for macros, each line must be terminated with backslash '\' character. If we use curly braces '{}' and the macros is ended with '}', then it may generate some error.

What is the proper command for multi-line comment?

To comment out multiple lines in Python, you can prepend each line with a hash ( # ).

How do you separate a multiline macro in?

"\ operator is used to separate a multiline macro in C language"

What is single line comment and multi-line comment?

Single-line comments begin with a double hyphen ( - - ) anywhere on a line and extend to the end of the line. Multi-line comments begin with a slash-asterisk ( /* ), end with an asterisk-slash ( */ ), and can span multiple lines.


2 Answers

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.

like image 127
Alexander Gelbukh Avatar answered Sep 20 '22 21:09

Alexander Gelbukh


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

#define SOME_BIG_MACRO(input)\   SOME_FUNCTION_CALL() /* this does... */ \    SOME_OTHER_FUNCTION_CALL() 
like image 30
Dr. Debasish Jana Avatar answered Sep 20 '22 21:09

Dr. Debasish Jana