<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US"><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>__VA_OPT__ wording clarifications</title>

<style type="text/css">
body { color: #000000; background-color: #FFFFFF; max-width: 40em; }
del, .del { text-decoration: line-through; color: #8B0040; }
ins, .ins { text-decoration: underline; color: #005100; }

p.example { margin-left: 2em; }
pre.example { margin-left: 2em; }
div.example { margin-left: 2em; }

code.extract { background-color: #F5F6A2; }
pre.extract { margin-left: 2em; background-color: #F5F6A2;
  border: 1px solid #E1E28E; }

p.function { }
.attribute { margin-left: 2em; }
.attribute dt { float: left; font-style: italic;
  padding-right: 1ex; }
.attribute dd { margin-left: 0em; }

blockquote.std, ul.std { color: #000000; background-color: #F1F1F1;
  border: 1px solid #D1D1D1;
  padding-left: 0.5em; padding-right: 0.5em; }
blockquote.std del, blockquote.std .del, ul.std del, ul.std .del { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC; }
blockquote.std ins, blockquote.std .ins, ul.std ins, ul.std .ins { text-decoration: underline;
  color: #000000; background-color: #C8FFC8; }
blockquote.stddel { text-decoration: line-through;
  color: #000000; background-color: #FFEBFF;
  border: 1px solid #ECD7EC;
  padding-left: 0.5em; padding-right: 0.5em; }
blockquote.stdins { text-decoration: none;
  color: #000000; background-color: #C8FFC8;
  border: 1px solid #B3EBB3;
  padding-left: 0.5em; padding-right: 0.5em; }

table { border: 1px solid black; border-spacing: 0px;
  margin-left: auto; margin-right: auto; }
th { text-align: left; vertical-align: top;
  padding-left: 0.8em; border: none; }
td { text-align: left; vertical-align: top;
  padding-left: 0.8em; border: none; }

table.table { border-spacing: 2px; border-collapse: separate; }
.table * th, .table * td { border: 1px solid black; }

table.frontmatter { border: 0; border-spacing: 0px; border-collapse: collapse; margin: 0; width: 619px; }
.frontmatter * td, .frontmatter * th { padding: 0px; }
.frontmatter * th { font-weight: inherit; text-align: left; vertical-align: top; }

ul.dash { list-style-type: none; }
ul.dash li:before { content: '\2014'; margin-left: -1em }

span.highlight { background-color: #7FDFFF }

.nowrap { white-space: nowrap; }
.pre { white-space: pre; }

#xins:checked ~ * ins { display: none; visibility: hidden }
#xdel:checked ~ * del { display: none; visibility: hidden }
</style>

<script type="text/javascript" src="https://cdn.rawgit.com/google/code-prettify/master/loader/run_prettify.js"></script>
</head>

<body>
<h1><code>__VA_OPT__</code> wording clarifications</h1>
<table class="frontmatter"><tbody>
    <tr>
        <th>Document number:</th>
        <td>P1042R0</td>
    </tr>
    <tr>
        <th>Date:</th>
        <td>2018-04-27</td>
    </tr>
    <tr>
        <th>Project:</th>
        <td>ISO/IEC JTC 1/SC 22/WG 21/C++</td>
    </tr>
    <tr>
        <th>Audience subgroup:</th>
        <td>Core</td>
    </tr>
    <tr>
        <th>Revises:</th>
        <td><i>None</i></td>
    </tr>
    <tr>
        <th>Reply-to:</th>
        <td><span class="nowrap">Hubert S.K. Tong</span> &lt;<a href="mailto:hubert.reinterpretcast@gmail.com">hubert.reinterpretcast@gmail.com</a>&gt;</td>
    </tr>
</tbody></table>

<h2>Background and Context</h2>
<p>A number of clarifications to the adopted wording were found necessary in private correspondence conducted among
<span class="nowrap">Thomas K&#x00f6;ppe</span>,
<span class="nowrap">Richard Smith</span>,
<span class="nowrap">Hubert Tong</span>, and
<span class="nowrap">Faisal Vali</span>.
Inquiries were also made on the <span class="nowrap">WG 14</span> and Core reflectors by
<span class="nowrap">Jens Gustedt</span>
and
<span class="nowrap">Jason Merrill</span>.
This paper clarifies the relationship between <code>__VA_OPT__</code> and
substitution, stringization, and concatenation; especially with regards to
placemarker tokens and white space.</p>

<h2>Wording</h2>
<p>The editing instructions within this document use N4741 as its base wording.</p>

<div><input type="checkbox" id="xins" /><label for="xins">Hide inserted text</label>
<input type="checkbox" id="xdel" /><label for="xdel">Hide deleted text</label><!--

--><p
>In subclause 19.3.1 [cpp.subst] paragraph 1, modify:</p
><blockquote class="std"><!--
TODO: Flip the two main cases around.
--><p
>After the arguments for the invocation of a function-like macro have been identified,
argument substitution takes place.
A parameter in the replacement list<ins>&#x2014;</ins><del>,
</del>unless <ins>it is
<code>__VA_OPT__(</code><i>content</i><code>)</code>,
</ins>preceded by a <code>#</code> or <code>##</code> preprocessing token<ins>,
</ins>or followed by a <code>##</code> preprocessing token (see below)<ins>&#x2014;</ins><del>,
</del>is replaced by the corresponding argument after all macros contained therein have been expanded<del
>. Before being substituted</del>, <ins>in which case,
</ins>each argument&#x2019;s preprocessing tokens are completely macro replaced
<ins>before being substituted
</ins>as if they formed the rest of the preprocessing file<del>;</del>
<ins>with </ins>no other preprocessing tokens <del>are </del><ins>being </ins>available.<ins>
The replacement for
<code>__VA_OPT__(</code><i>content</i><code>)</code>,
unless preceded by a <code>#</code> or <code>##</code> preprocessing token
or followed by a <code>##</code> preprocessing token,
is the preprocessing token sequence for the corresponding argument.</ins></p>
</blockquote><p
><i>Drafting note:</i> The above clarifies that <code>__VA_OPT__</code> expansions are not rescanned
prior to rescanning of the replacement list containing the instance of <code>__VA_OPT__</code>.</p
><!--

--><p
>Add an example to [cpp.subst] paragraph 1:</p
><blockquote class="stdins"><p class="pre example"
><code class="prettyprint language-cpp">#define RPAREN() (
#define G(Q) 42
#define F(R, X, ...)  __VA_OPT__(G R X) )
int x = F(RPAREN(), 0, &lt;:-);</code>  <i>// replaced by</i> <code>int x = 42;</code></p
></blockquote><!--

--><p
>In subclause 19.3.1 [cpp.subst] paragraph 3, modify:</p
><blockquote class="std"><p>The identifier <code>__VA_OPT__</code>
shall always occur as part of the <ins>preprocessing </ins>token sequence
<code>__VA_OPT__(</code><i>content</i><code>)</code>,
<span class="nowrap">[ &#x2026; ]</span></p>
</blockquote><!--

--><p
>In subclause 19.3.1 [cpp.subst] paragraph 3, modify:</p
><blockquote class="std"><p><span class="nowrap">[ &#x2026; ]</span>
The <ins>preprocessing </ins>token sequence
<code>__VA_OPT__(</code><i>content</i><code>)</code>
shall be treated as if it were a parameter, and
the preprocessing <del>tokens used to replace it
are </del><ins
>token sequence for the corresponding argument is </ins
>defined as follows.
If the
<del>variable arguments consist
</del><ins>substitution of <code>__VA_ARGS__</code> as neither an operand of
<code>#</code> nor <code>##</code> consists
</ins>of no <ins>preprocessing </ins>tokens, the
<del>replacement </del><ins>argument </ins
>consists of a single placemarker preprocessing token.
Otherwise, the
<del>replacement </del><ins>argument </ins
>consists of the results of the expansion of
<i>content</i> as the replacement list of the current function-like macro
before <ins>removal of placemarker tokens,
</ins>rescanning<ins>,</ins> and further replacement.<ins>
<span class="nowrap">[ <i>Note:</i></span>
The placemarker tokens are removed before stringization (19.3.2 [cpp.stringize]),
and can be removed by rescanning and further replacement (19.3.4 [cpp.rescan]).
<span class="nowrap"><i>&#x2014;end note</i> ]</span></ins></p>
</blockquote><!--

--><p
>Add the following examples to 19.3.1 [cpp.subst] paragraph 3:</p
><blockquote class="stdins"><p class="pre example"
><code class="prettyprint language-cpp">#define F(...) f(0 __VA_OPT__(,) __VA_ARGS__)
#define EMP
F(EMP)</code>  <i>// replaced by</i> <code>f(0)</code></p></blockquote><p
><i>Drafting note:</i> The above was requested on the
<span class="nowrap">WG 14</span>
reflector and differs from what current implementations do.</p><!--

--><blockquote class="stdins"><p class="pre example"
><code class="prettyprint language-cpp">#define H3(X, ...) #__VA_OPT__(X##X X##X)
H3(, 0)</code>  <i>// replaced by</i> <code>""</code></p></blockquote><p
><i>Drafting note:</i>
Clang replaces <code>H3</code> as presented.</p><!--

--><blockquote class="stdins"><p class="pre example"
><code class="prettyprint language-cpp">#define H4(X, ...) __VA_OPT__(a X ## X) ## b
H4(, 1)</code>  <i>// replaced by</i> <code>a b</code></p></blockquote><p
><i>Drafting note:</i>
The <code>H4</code> case differs from what current implementations do.
The removal of placemarkers was found to give surprising results for concatenation.</p
><!--

--><blockquote class="stdins"><p class="pre example"
><code class="prettyprint language-cpp">#define H5A(...) __VA_OPT__()/**/__VA_OPT__()
#define H5B(X) a ## X ## b
#define H5C(X) H5B(X)
H5C(H5A())</code>  <i>// replaced by</i> <code>ab</code></p>
</blockquote><!--

--><p
>In subclause 19.3.2 [cpp.stringize] paragraph 2, modify:</p
><blockquote class="std"><p><span class="nowrap">[ &#x2026; ]</span>
If, in the replacement list, a parameter is
immediately preceded by a <code>#</code> preprocessing token,
both are replaced by a single character string literal
preprocessing token that contains the spelling of the preprocessing token sequence
for the corresponding argument<ins> (excluding placemarker tokens)</ins>.
<ins>Let the <i>stringizing argument</i> be the preprocessing token sequence
for the corresponding argument with placemarker tokens removed.
</ins>Each occurrence of white space between the
<ins>stringizing </ins>argument&#x2019;s preprocessing tokens
becomes a single space character in the character string literal.
White space before the first preprocessing token and after
the last preprocessing token comprising the
<ins>stringizing </ins>argument is deleted.
Otherwise, the original spelling of each preprocessing token in the
<ins>stringizing </ins>argument is retained in the character string literal,
except for special handling for producing the spelling of string literals
and character literals: a <code>\</code> character is inserted before each
<code>"</code> and <code>\</code>
character of a character literal or string literal
(including the delimiting <code>"</code> characters).
If the replacement that results is not a valid character string literal,
the behavior is undefined.
The character string literal corresponding to an empty
<ins>stringizing </ins>argument is <code>""</code>.
<span class="nowrap">[ &#x2026; ]</span></p>
</blockquote></div></body></html>
