Skip to content

Commit 980486e

Browse files
authored
Merge 2018-06 CWG Motion 4
Fixes #2116
2 parents cdbd6c9 + ba6cd4c commit 980486e

File tree

1 file changed

+61
-29
lines changed

1 file changed

+61
-29
lines changed

source/preprocessor.tex

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -743,20 +743,32 @@
743743
\pnum
744744
After the arguments for the invocation of a function-like macro have
745745
been identified, argument substitution takes place.
746-
A parameter in the replacement list, unless preceded by a
747-
\tcode{\#}
748-
or
749-
\tcode{\#\#}
750-
preprocessing token or followed by a
751-
\tcode{\#\#}
752-
preprocessing token (see below),
753-
is replaced by the corresponding argument after all macros
754-
contained therein have been expanded.
755-
Before being substituted,
756-
each argument's preprocessing tokens are completely
757-
macro replaced as if they formed the rest of the
758-
preprocessing file;
759-
no other preprocessing tokens are available.
746+
For each parameter in the replacement list that is neither
747+
preceded by a \tcode{\#} or \tcode{\#\#} preprocessing token nor
748+
followed by a \tcode{\#\#} preprocessing token, the preprocessing tokens
749+
naming the parameter are replaced by a token sequence determined as follows:
750+
\begin{itemize}
751+
\item
752+
If the parameter is of the form
753+
\mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)},
754+
the replacement preprocessing tokens are the
755+
preprocessing token sequence for the corresponding argument.
756+
\item
757+
Otherwise, the replacement preprocessing tokens are the
758+
preprocessing tokens of corresponding argument after all
759+
macros contained therein have been expanded. The argument's
760+
preprocessing tokens are completely macro replaced before
761+
being substituted as if they formed the rest of the preprocessing
762+
file with no other preprocessing tokens being available.
763+
\end{itemize}
764+
\begin{example}
765+
\begin{codeblock}
766+
#define LPAREN() (
767+
#define G(Q) 42
768+
#define F(R, X, ...) __VA_OPT__(G R X) )
769+
int x = F(LPAREN(), 0, <:-); // replaced by \tcode{int x = 42;}
770+
\end{codeblock}
771+
\end{example}
760772

761773
\pnum
762774
\indextext{__VA_ARGS__@\mname{VA_ARGS}}%
@@ -767,8 +779,8 @@
767779
\pnum
768780
\indextext{__VA_OPT__@\mname{VA_OPT}}%
769781
The identifier \mname{VA_OPT}
770-
shall always occur as part of the token sequence
771-
\mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)},
782+
shall always occur as part of the preprocessing token sequence
783+
\mname{VA_OPT}\tcode{(}\placeholder{cont\-ent}\tcode{)},
772784
where \placeholder{content} is
773785
an arbitrary sequence of \grammarterm{preprocessing-token}s
774786
other than \mname{VA_OPT},
@@ -777,26 +789,33 @@
777789
If \placeholder{content} would be ill-formed
778790
as the replacement list of the current function-like macro,
779791
the program is ill-formed.
780-
The token sequence
792+
The preprocessing token sequence
781793
\mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)}
782794
shall be treated as if it were a parameter,
783-
and the preprocessing tokens used to replace it
784-
are defined as follows.
785-
If the variable arguments consist of no tokens,
786-
the replacement consists of
795+
and the preprocessing token sequence for the corresponding
796+
argument is defined as follows.
797+
If the substitution of \mname{VA_ARGS} as neither an operand
798+
of \tcode{\#} nor \tcode{\#\#} consists of no preprocessing tokens,
799+
the argument consists of
787800
a single placemarker preprocessing token~(\ref{cpp.concat}, \ref{cpp.rescan}).
788-
Otherwise, the replacement consists of
801+
Otherwise, the argument consists of
789802
the results of the expansion of \placeholder{content}
790803
as the replacement list of the current function-like macro
791-
before rescanning and further replacement.
804+
before removal of placemarker tokens, rescanning, and further replacement.
805+
\begin{note}
806+
The placemarker tokens are removed before stringization\iref{cpp.stringize},
807+
and can be removed by rescanning and further replacement\iref{cpp.rescan}.
808+
\end{note}
792809
\begin{example}
793810
\begin{codeblock}
794811
#define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)
795812
#define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)
796813
#define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ })
814+
#define EMP
797815

798816
F(a, b, c) // replaced by \tcode{f(0, a, b, c)}
799817
F() // replaced by \tcode{f(0)}
818+
F(EMP) // replaced by \tcode{f(0)}
800819

801820
G(a, b, c) // replaced by \tcode{f(0, a, b, c)}
802821
G(a, ) // replaced by \tcode{f(0, a)}
@@ -807,9 +826,20 @@
807826

808827
#define H1(X, ...) X __VA_OPT__(##) __VA_ARGS__ // ill-formed: \tcode{\#\#} may not appear at
809828
// the beginning of a replacement list\iref{cpp.concat}
810-
#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__
811829

830+
#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__
812831
H2(a, b, c, d) // replaced by \tcode{ab, c, d}
832+
833+
#define H3(X, ...) #__VA_OPT__(X##X X##X)
834+
H3(, 0) // replaced by \tcode{""}
835+
836+
#define H4(X, ...) __VA_OPT__(a X ## X) ## b
837+
H4(, 1) // replaced by \tcode{a b}
838+
839+
#define H5A(...) __VA_OPT__()@\tcode{/**/}@__VA_OPT__()
840+
#define H5B(X) a ## X ## b
841+
#define H5C(X) H5B(X)
842+
H5C(H5A()) // replaced by \tcode{ab}
813843
\end{codeblock}
814844
\end{example}
815845

@@ -832,13 +862,15 @@
832862
preprocessing token,
833863
both are replaced by a single character string literal preprocessing token that
834864
contains the spelling of the preprocessing token sequence for the
835-
corresponding argument.
836-
Each occurrence of white space between the argument's preprocessing
865+
corresponding argument (excluding placemarker tokens).
866+
Let the \defn{stringizing argument} be the preprocessing token sequence
867+
for the corresponding argument with placemarker tokens removed.
868+
Each occurrence of white space between the stringizing argument's preprocessing
837869
tokens becomes a single space character in the character string literal.
838870
White space before the first preprocessing token and after the last
839-
preprocessing token comprising the argument is deleted.
871+
preprocessing token comprising the stringizing argument is deleted.
840872
Otherwise, the original spelling of each preprocessing token in the
841-
argument is retained in the character string literal,
873+
stringizing argument is retained in the character string literal,
842874
except for special handling for producing the spelling of
843875
string literals and character literals:
844876
a
@@ -853,7 +885,7 @@
853885
characters).
854886
If the replacement that results is not a valid character string literal,
855887
the behavior is undefined. The character string literal corresponding to
856-
an empty argument is \tcode{""}.
888+
an empty stringizing argument is \tcode{""}.
857889
The order of evaluation of
858890
\tcode{\#}
859891
and

0 commit comments

Comments
 (0)