|
743 | 743 | \pnum
|
744 | 744 | After the arguments for the invocation of a function-like macro have
|
745 | 745 | 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} |
760 | 772 |
|
761 | 773 | \pnum
|
762 | 774 | \indextext{__VA_ARGS__@\mname{VA_ARGS}}%
|
|
767 | 779 | \pnum
|
768 | 780 | \indextext{__VA_OPT__@\mname{VA_OPT}}%
|
769 | 781 | 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{)}, |
772 | 784 | where \placeholder{content} is
|
773 | 785 | an arbitrary sequence of \grammarterm{preprocessing-token}s
|
774 | 786 | other than \mname{VA_OPT},
|
|
777 | 789 | If \placeholder{content} would be ill-formed
|
778 | 790 | as the replacement list of the current function-like macro,
|
779 | 791 | the program is ill-formed.
|
780 |
| -The token sequence |
| 792 | +The preprocessing token sequence |
781 | 793 | \mname{VA_OPT}\tcode{(}\placeholder{content}\tcode{)}
|
782 | 794 | 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 |
787 | 800 | a single placemarker preprocessing token~(\ref{cpp.concat}, \ref{cpp.rescan}).
|
788 |
| -Otherwise, the replacement consists of |
| 801 | +Otherwise, the argument consists of |
789 | 802 | the results of the expansion of \placeholder{content}
|
790 | 803 | 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} |
792 | 809 | \begin{example}
|
793 | 810 | \begin{codeblock}
|
794 | 811 | #define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)
|
795 | 812 | #define G(X, ...) f(0, X __VA_OPT__(,) __VA_ARGS__)
|
796 | 813 | #define SDEF(sname, ...) S sname __VA_OPT__(= { __VA_ARGS__ })
|
| 814 | +#define EMP |
797 | 815 |
|
798 | 816 | F(a, b, c) // replaced by \tcode{f(0, a, b, c)}
|
799 | 817 | F() // replaced by \tcode{f(0)}
|
| 818 | +F(EMP) // replaced by \tcode{f(0)} |
800 | 819 |
|
801 | 820 | G(a, b, c) // replaced by \tcode{f(0, a, b, c)}
|
802 | 821 | G(a, ) // replaced by \tcode{f(0, a)}
|
|
807 | 826 |
|
808 | 827 | #define H1(X, ...) X __VA_OPT__(##) __VA_ARGS__ // ill-formed: \tcode{\#\#} may not appear at
|
809 | 828 | // the beginning of a replacement list\iref{cpp.concat}
|
810 |
| -#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__ |
811 | 829 |
|
| 830 | +#define H2(X, Y, ...) __VA_OPT__(X ## Y,) __VA_ARGS__ |
812 | 831 | 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} |
813 | 843 | \end{codeblock}
|
814 | 844 | \end{example}
|
815 | 845 |
|
|
832 | 862 | preprocessing token,
|
833 | 863 | both are replaced by a single character string literal preprocessing token that
|
834 | 864 | 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 |
837 | 869 | tokens becomes a single space character in the character string literal.
|
838 | 870 | 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. |
840 | 872 | 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, |
842 | 874 | except for special handling for producing the spelling of
|
843 | 875 | string literals and character literals:
|
844 | 876 | a
|
|
853 | 885 | characters).
|
854 | 886 | If the replacement that results is not a valid character string literal,
|
855 | 887 | the behavior is undefined. The character string literal corresponding to
|
856 |
| -an empty argument is \tcode{""}. |
| 888 | +an empty stringizing argument is \tcode{""}. |
857 | 889 | The order of evaluation of
|
858 | 890 | \tcode{\#}
|
859 | 891 | and
|
|
0 commit comments