<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2797: Trait precondition violations</title>
<meta property="og:title" content="Issue 2797: Trait precondition violations">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2797.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Resolved">Resolved</a> status.</em></p>
<h3 id="2797"><a href="lwg-defects.html#2797">2797</a>. Trait precondition violations</h3>
<p><b>Section:</b> 21.3.3 <a href="https://wg21.link/meta.type.synop">[meta.type.synop]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Russia <b>Opened:</b> 2016-11-09 <b>Last modified:</b> 2018-11-12</p>
<p><b>Priority: </b>2
</p>
<p><b>View other</b> <a href="lwg-index-open.html#meta.type.synop">active issues</a> in [meta.type.synop].</p>
<p><b>View all other</b> <a href="lwg-index.html#meta.type.synop">issues</a> in [meta.type.synop].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
<b>Addresses RU 2</b>
<p/>
Failed prerequirement for the type trait must result in ill-formed program.
Otherwise hard detectable errors will happen:
</p>
<blockquote><pre>
#include &lt;type_traits&gt;

struct foo;

void damage_type_trait() {
  // must be ill-formed
  std::is_constructible&lt;foo, foo&gt;::value;
}

struct foo{};

int main() {
  static_assert(
    // produces invalid result
    std::is_constructible&lt;foo, foo&gt;::value,
    "foo must be constructible from foo"
  );
}
</pre></blockquote>
<p>
Suggested resolution:
<p/>
Add to the end of the [meta.type.synop] section:
</p>
<blockquote><p>
<ins>Program is ill-formed if precondition for the type trait is violated.</ins>
</p></blockquote>

<p><i>[2016-11-09, Jonathan provides wording]</i></p>


<p><i>[Issues Telecon 16-Dec-2016]</i></p>

<p>Priority 2</p>

<p><i>[2017-03-02, Daniel comments]</i></p>

<p>
LWG <a href="lwg-active.html#2939" title="Some type-completeness constraints of traits are overspecified (Status: Open)">2939</a><sup><a href="https://cplusplus.github.io/LWG/issue2939" title="Latest snapshot">(i)</a></sup> has been created to signal that some of our current type trait constraints are
not quite correct and I recommend <em>not</em> to enforce the required diagnostics for traits that
are sensitive to mismatches of the current approximate rules.
</p>

<p><i>[2017-03-03, Kona Friday morning]</i></p>

<p>Unanimous consent to adopt this for C++17, but due to a misunderstanding, it wasn't on the ballot</p>
<p>Setting status to 'Ready' so we'll get it in immediately post-C++17</p>

<p><i>[2017-06-15 request from Daniel]</i></p>

<p>I don't believe that this should be "Ready"; I added the extra note to LWG 2797 *and* added the new issue <a href="lwg-active.html#2939" title="Some type-completeness constraints of traits are overspecified (Status: Open)">2939</a><sup><a href="https://cplusplus.github.io/LWG/issue2939" title="Latest snapshot">(i)</a></sup> exactly to *prevent* 2797 being accepted for C++17</p>
<p>Setting status back to 'Open'</p>


<p><i>[2018-08 Batavia Monday issue discussion]</i></p>

<p>Issues <a href="lwg-defects.html#2797" title="Trait precondition violations (Status: Resolved)">2797</a><sup><a href="https://cplusplus.github.io/LWG/issue2797" title="Latest snapshot">(i)</a></sup>, <a href="lwg-active.html#2939" title="Some type-completeness constraints of traits are overspecified (Status: Open)">2939</a><sup><a href="https://cplusplus.github.io/LWG/issue2939" title="Latest snapshot">(i)</a></sup>, <a href="lwg-defects.html#3022" title="is_convertible&lt;derived*, base*&gt; may lead to ODR (Status: Resolved)">3022</a><sup><a href="https://cplusplus.github.io/LWG/issue3022" title="Latest snapshot">(i)</a></sup>, and <a href="lwg-active.html#3099" title="is_assignable&lt;Incomplete&amp;, Incomplete&amp;&gt; (Status: Open)">3099</a><sup><a href="https://cplusplus.github.io/LWG/issue3099" title="Latest snapshot">(i)</a></sup> are all closely related. Walter to write a paper resolving them.</p>

<p><i>[2018-11-11 Resolved by <a href="https://wg21.link/P1285R0">P1285R0</a>, adopted in San Diego.]</i></p>



<p id="res-2797"><b>Proposed resolution:</b></p>
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Add a new paragraph after 21.3.6.4 <a href="https://wg21.link/meta.unary.prop">[meta.unary.prop]</a> paragraph 3:</p>
<blockquote>
<p>
<ins>-?- If an instantiation of any template declared in this subclause fails
to meet the preconditions, the program is ill-formed.</ins>
</p>
</blockquote>
</li>

<li><p>Change the specification for <code>alignment_of</code> in Table 39 in 21.3.7 <a href="https://wg21.link/meta.unary.prop.query">[meta.unary.prop.query]</a>:</p>

<blockquote>
<table border="1">
<caption>Table 39 &mdash; Type property queries</caption>
<tr>
<td>
<code>template &lt;class T&gt; struct alignment_of;</code>
</td>
<td>
<code>alignof(T)</code>.<br/>
<i>Requires:</i> <code>alignof(T)</code> shall be a valid expression (5.3.6)<ins>,
otherwise the program is ill-formed</ins>
</td>
</tr>

</table>
</blockquote>

</li>

<li><p>Change the specification for <code>is_base_of</code>, <code>is_convertible</code>, <code>is_callable</code>, and
<code>is_nothrow_callable</code> in Table 40 in 21.3.8 <a href="https://wg21.link/meta.rel">[meta.rel]</a>:</p>

<blockquote>
<table border="1">
<caption>Table 40 &mdash; Type relationship predicates</caption>

<tr>
<td colspan="3" align="center">
<code>[&hellip;]</code>
</td>
</tr>

<tr>
<td>
<code>template &lt;class Base, class<br/>
Derived&gt;<br/>
struct is_base_of;</code>
</td>
<td>
[&hellip;]
</td>
<td>
If <code>Base</code> and <code>Derived</code> are<br/>
non-union class types and are<br/>
different types (ignoring possible<br/>
<i>cv</i>-qualifiers) then <code>Derived</code> shall<br/>
be a complete type<ins>, otherwise the program is ill-formed</ins>.<br/>
[<i>Note:</i> Base classes that<br/>
are private, protected, or ambiguous are,<br/>
nonetheless, base classes. &mdash; <i>end note</i>]
</td>
</tr>

<tr>
<td>
<code>template &lt;class From, class To&gt;<br/>
struct is_convertible;</code>
</td>
<td>
<i>see below</i>
</td>
<td>
<code>From</code> and <code>To</code> shall be complete<br/>
types, arrays of unknown bound,<br/>
or (possibly <i>cv</i>-qualified) <code>void</code><br/>
types<ins>, otherwise the program is<br/>
ill-formed</ins>.
</td>
</tr>

<tr>
<td>
<code>template &lt;class Fn, class...<br/>
ArgTypes, class R&gt;<br/>
struct is_callable&lt;<br/>
Fn(ArgTypes...), R&gt;;</code>
</td>
<td>
[&hellip;]
</td>
<td>
<code>Fn</code>, <code>R</code>, and all types in the<br/>
parameter pack <code>ArgTypes</code> shall<br/>
be complete types, (possibly<br/>
<i>cv</i>-qualified) <code>void</code>, or arrays of<br/>
unknown bound<ins>,<br/>
otherwise the program is ill-formed</ins>.
</td>
</tr>

<tr>
<td>
<code>template &lt;class Fn, class...<br/>
ArgTypes, class R&gt;<br/>
struct is_nothrow_callable&lt;<br/>
Fn(ArgTypes...), R&gt;;</code>
</td>
<td>
[&hellip;]
</td>
<td>
<code>Fn</code>, <code>R</code>, and all types in the<br/>
parameter pack <code>ArgTypes</code> shall<br/>
be complete types, (possibly<br/>
<i>cv</i>-qualified) <code>void</code>, or arrays of<br/>
unknown bound<ins>,<br/>
otherwise the program is ill-formed</ins>.
</td>
</tr>

</table>
</blockquote>

</li>

<li><p>Add a new paragraph after 21.3.9 <a href="https://wg21.link/meta.trans">[meta.trans]</a> paragraph 2:</p>
<blockquote>
<p>
-2- Each of the templates in this subclause shall be a <code>TransformationTrait</code> (20.15.1).
<p/>
<ins>-?- If an instantiation of any template declared in this subclause fails
to meet the <i>Requires:</i> preconditions, the program is ill-formed.</ins>
</p>
</blockquote>
</li>


</ol>





</body>
</html>
