Skip to content

Commit d4ff113

Browse files
tkoeppezygoloid
authored andcommitted
P1042R1 __VA_OPT__ wording clarifications
1 parent cdbd6c9 commit d4ff113

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

source/preprocessor.tex

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -743,20 +743,31 @@
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
746+
A parameter in the replacement list --- unless it is
747+
\mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)}, preceded by a
747748
\tcode{\#}
748749
or
749750
\tcode{\#\#}
750-
preprocessing token or followed by a
751+
preprocessing token, or followed by a
751752
\tcode{\#\#}
752-
preprocessing token (see below),
753+
preprocessing token (see below) ---
753754
is replaced by the corresponding argument after all macros
754-
contained therein have been expanded.
755-
Before being substituted,
755+
contained therein have been expanded, in which case,
756756
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.
757+
macro replaced before being substituted as if they formed the rest of the
758+
preprocessing file with
759+
no other preprocessing tokens being available.
760+
The replacement for \mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)}, unless preceded by a
761+
\tcode{\#} or \tcode{\#\#} preprocessing token or followed by a \tcode{\#\#}
762+
preprocessing token, is the preprocessing token sequence for the corresponding argument.
763+
\begin{example}
764+
\begin{codeblock}
765+
#define LPAREN() (
766+
#define G(Q) 42
767+
#define F(R, X, ...) __VA_OPT__(G R X) )
768+
int x = F(LPAREN(), 0, <:-); // replaced by \tcode{int x = 42;}
769+
\end{codeblock}
770+
\end{example}
760771

761772
\pnum
762773
\indextext{__VA_ARGS__@\mname{VA_ARGS}}%
@@ -767,8 +778,8 @@
767778
\pnum
768779
\indextext{__VA_OPT__@\mname{VA_OPT}}%
769780
The identifier \mname{VA_OPT}
770-
shall always occur as part of the token sequence
771-
\mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)},
781+
shall always occur as part of the preprocessing token sequence
782+
\mname{VA_OPT}\tcode{(}\placeholder{cont\-ent}\tcode{)},
772783
where \placeholder{content} is
773784
an arbitrary sequence of \grammarterm{preprocessing-token}s
774785
other than \mname{VA_OPT},
@@ -777,26 +788,33 @@
777788
If \placeholder{content} would be ill-formed
778789
as the replacement list of the current function-like macro,
779790
the program is ill-formed.
780-
The token sequence
791+
The preprocessing token sequence
781792
\mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)}
782793
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
794+
and the preprocessing token sequence for the corresponding
795+
argument is defined as follows.
796+
If the substitution of \mname{VA_ARGS} as neither an operand
797+
of \tcode{\#} nor \tcode{\#\#} consists of no preprocessing tokens,
798+
the argument consists of
787799
a single placemarker preprocessing token~(\ref{cpp.concat}, \ref{cpp.rescan}).
788-
Otherwise, the replacement consists of
800+
Otherwise, the argument consists of
789801
the results of the expansion of \placeholder{content}
790802
as the replacement list of the current function-like macro
791-
before rescanning and further replacement.
803+
before removal of placemarker tokens, rescanning, and further replacement.
804+
\begin{note}
805+
The placemarker tokens are removed before stringization\iref{cpp.stringize},
806+
and can be removed by rescanning and further replacement\iref{cpp.rescan}.
807+
\end{note}
792808
\begin{example}
793809
\begin{codeblock}
794810
#define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)
795811
#define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)
796812
#define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ })
813+
#define EMP
797814

798815
F(a, b, c) // replaced by \tcode{f(0, a, b, c)}
799816
F() // replaced by \tcode{f(0)}
817+
F(EMP) // replaced by \tcode{f(0)}
800818

801819
G(a, b, c) // replaced by \tcode{f(0, a, b, c)}
802820
G(a, ) // replaced by \tcode{f(0, a)}
@@ -807,9 +825,20 @@
807825

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

829+
#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__
812830
H2(a, b, c, d) // replaced by \tcode{ab, c, d}
831+
832+
#define H3(X, ...) #__VA_OPT__(X##X X##X)
833+
H3(, 0) // replaced by \tcode{""}
834+
835+
#define H4(X, ...) __VA_OPT__(a X ## X) ## b
836+
H4(, 1) // replaced by \tcode{a b}
837+
838+
#define H5A(...) __VA_OPT__()@\tcode{/**/}@__VA_OPT__()
839+
#define H5B(X) a ## X ## b
840+
#define H5C(X) H5B(X)
841+
H5C(H5A()) // replaced by \tcode{ab}
813842
\end{codeblock}
814843
\end{example}
815844

@@ -832,13 +861,15 @@
832861
preprocessing token,
833862
both are replaced by a single character string literal preprocessing token that
834863
contains the spelling of the preprocessing token sequence for the
835-
corresponding argument.
836-
Each occurrence of white space between the argument's preprocessing
864+
corresponding argument (excluding placemarker tokens).
865+
Let the \defn{stringizing argument} be the preprocessing token sequence
866+
for the corresponding argument with placemarker tokens removed.
867+
Each occurrence of white space between the stringizing argument's preprocessing
837868
tokens becomes a single space character in the character string literal.
838869
White space before the first preprocessing token and after the last
839-
preprocessing token comprising the argument is deleted.
870+
preprocessing token comprising the stringizing argument is deleted.
840871
Otherwise, the original spelling of each preprocessing token in the
841-
argument is retained in the character string literal,
872+
stringizing argument is retained in the character string literal,
842873
except for special handling for producing the spelling of
843874
string literals and character literals:
844875
a
@@ -853,7 +884,7 @@
853884
characters).
854885
If the replacement that results is not a valid character string literal,
855886
the behavior is undefined. The character string literal corresponding to
856-
an empty argument is \tcode{""}.
887+
an empty stringizing argument is \tcode{""}.
857888
The order of evaluation of
858889
\tcode{\#}
859890
and

0 commit comments

Comments
 (0)