<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en-CA">
  <head>
    <title>Explicit concept expressions</title>
    <style type="text/css">
      html { margin: 0; padding: 0; color: black; background-color: white; }
      body { padding: 2em; font-size: medium; font-family: "DejaVu Serif", serif; line-height: 150%; }
      code { font-family: "DejaVu Sans Mono", monospace; color: #006; }

      h1, h2, h3 { margin: 1.5em 0 .75em 0; line-height: 125%; }

      sup, sub { line-height: 0; }

      div.code { white-space: pre-line; font-family: "DejaVu Sans Mono", monospace;
                 border: thin solid #E0E0E0; background-color: #F8F8F8; padding: 1em;
                 border-radius: 4px; }

      div.strictpre { white-space: pre; }

      div.code em { font-family: "DejaVu Serif", serif; }

      table { border-top: 2px solid black; border-bottom: 2px solid black; border-collapse: collapse; margin: 3em auto; }
      thead th { border-bottom: 2px solid black; }
      th, td { text-align: left; padding: 1ex 1ex 1ex 5em; }
      th:first-child, td:first-child { padding-left: 1ex; }

      tr.new td { background-color: #EFE; }
      td.new:after { content: "new!"; font-family: "DejaVu Sans", sans-serif; font-weight: bold; font-size: xx-small;
                     vertical-align: top; top: -1em; right: -1em; position: relative; float: right; color: #090; }

      .docinfo { float: right }
      .docinfo p { margin: 0; text-align:right; }
      .docinfo address { font-style: normal; }

      .quote { display: inline-block; clear: both; margin-left: 1ex;
               border: thin solid #E0E0E0; background-color: #F8F8F8; padding: 1ex; }

      /*  Use DIV[insert] and DIV[delete] if the entire paragraph is added or removed; otherwise
       *  use DIV[modify] and use INS/DEL elements to mark up individual changes.
       */

      div.insert { border-left: thick solid #0A0; border-right: thick solid #0A0; padding: 0 1em; }
      div.modify { border-left: thick solid #999; border-right: thick solid #999; padding: 0 1em; }
      div.delete { border-left: thick solid #A00; border-right: thick solid #A00; padding: 0 1em; }

      .comment { color: #753; }

      del { color: #A00; text-decoration: line-through; }
      ins { color: #090; }
      ins code, del code { color: inherit; }

      .box { border: thin black solid; padding: 2px; }

      .nowrap { white-space: nowrap; }
    </style>
  </head>
  <body>
    <div class="docinfo">
      <p>ISO/IEC JTC 1/SC 22/WG 21 P1013R0</p>
      <p>Date: 2018-04-02</p>
      <p>To: EWG</p>
      <address>
        Thomas K&ouml;ppe &lt;<a href="mailto:tkoeppe@google.com">tkoeppe@google.com</a>&gt;<br />
        Hubert S.K. Tong &lt;<a href="mailto:hubert.reinterpretcast@gmail.com">hubert.reinterpretcast@gmail.com</a>&gt;
      </address>
    </div>

    <h1>Explicit concept expressions</h1>

    <h2>Abstract</h2>

    <p>We propose to remove from the working paper the fact that plain concept names can be
      boolean prvalue expressions and instead introduce a mildly more verbose
      &ldquo;<em>simple-requires-expression</em>&rdquo;. Instead of
      <code class="nowrap">bool b = Sortable&lt;MyType&gt;;</code> we propose
      <code class="nowrap">bool b = <strong>requires</strong> Sortable&lt;MyType&gt;;</code>
      This change removes unfortunate corner cases and paves the way for future extensions.</p>

    <h2>Contents</h2>
    <!-- fgrep -e "<h2 id=" concept_novalue.html | sed -e 's/.*id="\(.*\)">\(.*\)<\/h2>/<li><a href="#\1">\2<\/a><\/li>/g' -->
    <ol>
      <li><a href="#beforeafter">Before/After</a></li>
      <li><a href="#motivation">Problem and motivation</a></li>
      <li><a href="#proposal">Proposal</a></li>
      <li><a href="#alt">Alternatives</a></li>
      <li><a href="#impact">Impact on the past and future</a></li>
      <li><a href="#wording">Proposed wording</a></li>
      <li><a href="#ack">Acknowledgements</a></li>
    </ol>

    <!-- h2 id="history">Revision history</h2>

    <ul>
      <li>This version: Initial proposal</li>
    </ul -->

    <h2 id="beforeafter">Before/After</h2>

    <table>
      <colgroup>
        <col style="width: 37em">
        <col style="width: 42em">
      </colgroup>

      <thead>
        <tr><th>Before the proposal</th><th>With the proposal</th></tr>
      </thead>

      <tbody>
        <tr>
          <td><div class="code">template &lt;typename T&gt; concept Foo = <span class="comment">/* ... */</span>;

template &lt;typename T&gt; void f(T) {
<span style="color: #A00">&nbsp; static_assert(Foo&lt;T&gt;);</span>
<span style="color: #A00">&nbsp; if constexpr (Foo&lt;T&gt;)</span> { <span class="comment">/* ... */</span> }
}</div></td>
          <td><div class="code">template &lt;typename T&gt; concept Foo = <span class="comment">/* ... */</span>;

template &lt;typename T&gt; void f(T) {
<span style="color: #0A0">&nbsp; static_assert(requires Foo&lt;T&gt;);</span>
<span style="color: #0A0">&nbsp; if constexpr (requires Foo&lt;T&gt;)</span> { <span class="comment">/* ... */</span> }
}</div></td>
        </tr>
      </tbody>
    </table>

    <h2 id="motivation">Problem and motivation</h2>

    <p>Consider a simple type concept:</p>
    <div class="code">template &lt;typename T, typename U = T&gt; concept Foo = <span class="comment">/* ... */</span>;</div>
    <p>For a given type, say <code>int</code>, the current working paper makes <code>Foo&lt;int&gt;</code>
      a boolean prvalue. This creates a curious corner case if we consider future syntax extensions for
      abbreviated function templates. Consider the following code:</p>
    <div class="code">template &lt;typename T&gt; bool x(Foo&lt;T&gt;);</div>
    <p>There are two plausible meanings this code could have (assuming further work in the direction
      of <a href="http://wg21.link/p0745r0">P0745R0</a>):</p>
    <div class="code">template &lt;typename T&gt;<!--
--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!-- typename
--> &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<!-- U&gt; requires
--> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;<!-- Foo&lt;T, U&gt;
--> bool x = Foo&lt;T&gt;; &nbsp; <span class="comment">// #1 (variable)</span>

template &lt;typename T, typename U&gt; requires Foo&lt;T, U&gt; bool x(U u); &nbsp; &nbsp; &nbsp; <span class="comment">// #2 (function)</span></div>
    <p>This is kind of an inverse of the &ldquo;most vexing parse&rdquo;. In the status quo of
      the working paper, the meaning is that of case #1 (a variable declaration); whereas at
      least some have expressed that the preferable meaning would be that of case #2 (a function
      declaration).  This latter case is the one that is consistent with dropping constraints:
      <code>template &lt;typename T&gt; bool x(T)</code> is already a function declaration in
      the status quo.</p>

    <p>We can avoid this problem if we make it so that plain concept names do not form prvalue
      expressions, thus spuriously occupying a &ldquo;privileged&rdquo; syntax.  If we retain
      the status quo and later discover that this problem is serious, it will be a breaking
      change to remove or change the behaviour. On the other hand, if we remove the behaviour
      now and later discover that we actually do need it, we can easily add it back in without
      breaking code written to a level of C++ specified by an International Standard.</p>

    <h2 id="proposal">Proposal</h2>

    <p>We propose that an <em>id-expression</em> ([expr.prim.id]) that denotes the
      specialization of a concept no longer results in a prvalue except in specific contexts
      where we can expect normalization to consider it to be an atomic constraint.  To make up
      for the lost functionality, we introduce a new kind of expression: a
      &ldquo;<em>simple-requires-expression</em>&rdquo;, which turns a specialization of a
      concept into the boolean value indicating its constraint satisfaction.</p>

    <h2 id="alt">Alternatives</h2>

    <p>Instead of making concepts not be expressions, we could make them expressions precisely
      when they are &ldquo;named fully&rdquo; or &ldquo;fully specialized&rdquo;, but treat
      them as type placeholders when they are only &ldquo;partially bound&rdquo;.  A
      hypothetical short function template syntax might then fit in like this:</p>

    <div class="code">template &lt;typename A, typename B&gt; concept Foo = <span class="comment">/* ... */</span>;

      bool x(Foo&lt;int, char&gt;);&nbsp; <span class="comment">// variable, equiv. to bool x = Foo&lt;int, char&gt;;</span>
      bool y(Foo&lt;char&gt;); &nbsp;&nbsp;&nbsp;&nbsp;<!--
      int, --> &nbsp;<!-- two spaces
      --><span class="comment">// function, equiv. to template &lt;typename T&gt; requires Foo&lt;char, T&gt; bool y(T)</span></div>

    <p>In this approach, it is necessary to know the declaration of <code>Foo</code> in order to
      know whether <code class="nowrap">bool y(Foo&lt;char&gt;)</code> declares a variable or a
      function, with the usual concerns about maintenance, default arguments, etc.</p>

    <p>This approach presupposes a detail of a future, not-yet-made design. It is a plausible
      approach, but at the same time this direction is compatible with our main proposal: we can
      first remove the valueness of plain concept names, and later bring it back with the
      semantics described here.</p>

    <p>The proposed new <em>simple-requires-expression</em> is not strictly required,
      since a boolean value can also be obtained from an (ordinary) <em>requires-expression</em>
      such as &ldquo;<code>requires { requires Foo&lt;T&gt;; }</code>&rdquo;.
      We feel that such an expression would be somewhat unwieldy in contexts such as those of
      constexpr if and static assertion, and that the new <em>simple-requires-expression</em>
      will fit in more naturally.</p>

    <h2 id="impact">Impact on the past and future</h2>

    <p>There is no impact on the Standard, since the proposal modifies a feature that has not
      yet been standardized.</p>

    <p>Removing concept <i>id-expressions</i> now means that we do not standardize a feature
      that we may later regret and cannot change without a break. On the other hand, adding the
      removed feature back in later, if and when we do need it, is straightforward.</p>

    <h2 id="wording">Proposed wording</h2>

    <p>[to be fleshed out]</p>
    <div class="modify"><em>requires-expression</em>:<br />
      &nbsp; &nbsp; <code>requires</code> <em>requirement-parameter-list</em><sub>opt</sub> <em>requirement-body</em><br>
      <br>
      <ins><em>simple-requires-expression</em>:</ins><br>
      &nbsp; &nbsp; <ins><code>requires</code> <em>nested-name-specifier</em><sub>opt</sub> <em>simple-template-id</em></ins>
    </div>

    <p>An <del><em>id-expression</em> that</del><ins><em>simple-requires-expression</em>
      whose <em>simple-template-id</em></ins> denotes the specialization of a concept
      [temp.concept] results in a prvalue of type <code>bool</code>. The expression
      is <code>true</code> if the concept&rsquo;s normalized <em>constraint-expression</em>
      [temp.constr.decl] is satisfied [temp.constr.constr] by the specified template arguments
      and <code>false</code> otherwise.</p>

    <div class="code">template&lt;typename T&gt; concept C = true;
      static_assert(<ins>requires </ins>C&lt;int&gt;);          // OK</div>

    <p>[<em>Note</em>: A concept&rsquo;s constraints are also considered
      when using a template name [temp.names] and during overload resolution [over],
      and they are compared during the the partial ordering of constraints [temp.constr.order].
      &mdash;<em>end note</em>]</p>

    <h2 id="ack">Acknowledgements</h2>

    <p>Many thanks Andrew Sutton for valuable discussion.</p>

  </body>
</html>
