问题
I saw some examples in CPP manual where we can write macros body in many lines without the backslash.
#define strange(file) fprintf (file, "%s %d",
...
strange(stderr) p, 35)
output:
fprintf (stderr, "%s %d", p, 35)
Are they special cases like directives inside arguments macros or is it allowed only for #define ?
For include directives It must be always declared on one line if I am not wrong.
Edit:
From https://gcc.gnu.org/onlinedocs/cpp/Directives-Within-Macro-Arguments.html
3.9 Directives Within Macro Arguments
Occasionally it is convenient to use preprocessor directives within the arguments of a macro. The C and C++ standards declare that behavior in these cases is undefined. GNU CPP processes arbitrary directives within macro arguments in exactly the same way as it would have processed the directive were the function-like macro invocation not present.
If, within a macro invocation, that macro is redefined, then the new definition takes effect in time for argument pre-expansion, but the original definition is still used for argument replacement. Here is a pathological example:
#define f(x) x x f (1 #undef f #define f 2 f)
which expands to
1 2 1 2
with the semantics described above.
The example is on many lines.
回答1:
Multi-line macro definitions without backslash-newline
Since comments are replaced by spaces in translation phase 3:
- The source file is decomposed into preprocessing tokens7) and sequences of white-space characters (including comments). A source file shall not end in a partial preprocessing token or in a partial comment. Each comment is replaced by one space character. New-line characters are retained. Whether each nonempty sequence of white-space characters other than new-line is retained or replaced by one space character is implementation-defined.
and the preprocessor runs as phase 4:
- Preprocessing directives are executed, macro invocations are expanded, and
_Pragma
unary operator expressions are executed. If a character sequence that matches the syntax of a universal character name is produced by token concatenation (6.10.3.3), the behavior is undefined. A#include
preprocessing directive causes the named header or source file to be processed from phase 1. through phase 4, recursively. All preprocessing directives are then deleted.
it is possible, but absurd, to write a multi-line macro like this:
#include <stdio.h>
#define possible_but_absurd(a, b) /* comments
*/ printf("are translated"); /* in phase 3
*/ printf(" before phase %d", a); /* (the preprocessor)
*/ printf(" is run (%s)\n", b); /* but why abuse the system? */
int main(void)
{
printf("%s %s", "Macros can be continued without backslashes",
"because comments\n");
possible_but_absurd(4, "ISO/IEC 9899:2011,\nSection 5.1.1.2"
" Translation phases");
return 0;
}
which, when run, states:
Macros can be continued without backslashes because comments
are translated before phase 4 is run (ISO/IEC 9899:2011,
Section 5.1.1.2 Translation phases)
Backslash-newline in macro definitions
Translation phases 1 and 2 are also somewhat relevant:
- Physical source file multibyte characters are mapped, in an implementation-defined manner, to the source character set (introducing new-line characters for end-of-line indicators) if necessary. Trigraph sequences are replaced by corresponding single-character internal representations.
The trigraph replacement is nominally relevant because ??/
is the trigraph for a backslash.
- 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. A source file that is not empty shall end in a new-line character, which shall not be immediately preceded by a backslash character before any such splicing takes place.
This tells you that by the time phase 4 (the preprocessor) is run, macro definitions are on a single (logical) line — the trailing backslash-newline combinations have been deleted.
The standard notes that the phases are 'as if' — the behaviour of the compiler must be as if it went through the separate phases, but many implementations do not formally separate them out fully.
Avoid the GCC extension
The expanded example (quote from the GCC manual) has the invocation spread over many lines, but the definition is strictly on one line. (This much is not a GCC extension but completely standard behaviour.)
Note that if you're remotely sane, you'll ignore the possibility of putting preprocessing directives within the invocation of a macro (the #undef
and #define
in the example). It is a GCC extension and totally unportable. The standard says that the behaviour is undefined.
Annex J.2 Undefined behavior
- There are sequences of preprocessing tokens within the list of macro arguments that would otherwise act as preprocessing directives (6.10.3).
回答2:
No, it is not possible. Line splices are processed before each line to be interpreted either as a code text line or a preprocessing directive and inside the preprocessor step number 4(the effective preprocess algorithm) arrive no line splice, as they were eliminated before.
#define strange(file) fprintf (file, "%s %d",
Your define is interpreted as a function macro that have the preprocessor replace the call strange(stderr)
with fprintf (stderr, "%s %d",
. The preprocessor has no knowledge of C syntax and semantics, it's another language, non-turing-complete.
回答3:
No. You may not. You must use backslash as a line continuation escape character for multi-line preprocessor macros.
来源:https://stackoverflow.com/questions/41710558/can-we-write-a-macro-in-many-lines-without-the-backslash-at-the-end