<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 3193: Mandates: and Expects: elements are not defined for types</title>
<meta property="og:title" content="Issue 3193: Mandates: and Expects: elements are not defined for types">
<meta property="og:description" content="C++ library issue. Status: New">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue3193.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#New">New</a> status.</em></p>
<h3 id="3193"><a href="lwg-active.html#3193">3193</a>. <i>Mandates:</i> and <i>Expects:</i> elements are not defined for types</h3>
<p><b>Section:</b> 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a> <b>Status:</b> <a href="lwg-active.html#New">New</a>
 <b>Submitter:</b> Daniel Kr&uuml;gler <b>Opened:</b> 2019-03-04 <b>Last modified:</b> 2020-06-11</p>
<p><b>Priority: </b>3
</p>
<p><b>View other</b> <a href="lwg-index-open.html#structure.specifications">active issues</a> in [structure.specifications].</p>
<p><b>View all other</b> <a href="lwg-index.html#structure.specifications">issues</a> in [structure.specifications].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#New">New</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The working paper uses the special elements <i>Mandates:</i>, <i>Expects:</i> as well as
<i>Requires:</i> to types, albeit 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a> defines them
only for functions, for example 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a> sub-bullet (3.4):
</p>
<blockquote><p>
<i>Expects:</i> the conditions (sometimes termed preconditions) that the function assumes to hold whenever
it is called.
</p></blockquote>
<p>
Examples for such usages on types are (from <a href="https://wg21.link/n4800">N4800</a>):
</p>
<ul>
<li><p>27.2.3 <a href="https://wg21.link/char.traits.typedefs">[char.traits.typedefs]</a> for types <code>int_type</code> and <code>state_type</code></p></li>
<li><p>22.3.4 <a href="https://wg21.link/pair.astuple">[pair.astuple]</a> for <code>tuple_element&lt;I, pair&lt;T1, T2&gt;&gt;::type</code></p></li>
<li><p>22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a> for <code>tuple_element&lt;I, tuple&lt;Types...&gt;&gt;::type</code></p></li>
<li><p>22.4.11 <a href="https://wg21.link/tuple.traits">[tuple.traits]</a> for <code>uses_allocator&lt;tuple&lt;Types...&gt;, Alloc&gt;</code></p></li>
<li><p>Table 62 &mdash; "Container requirements" for type <code>XX::value_type</code></p></li>
<li><p>Table 65 &mdash; "Allocator-aware container requirements" for type <code>allocator_type</code></p></li>
<li><p>Table 69 &mdash; "Associative container requirements" for types <code>X::value_type</code> and <code>X::key_compare</code></p></li>
<li><p>Table 70 &mdash; "Unordered associative container requirements" for types <code>X::value_type</code> and <code>X::key_equal</code></p></li>
</ul>
<p>
Instead of replacing these elements usages for these places by extra wording to reach the same effects I
recommend to update instead 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a> to ensure that requirement-expressing elements
are defined in a way that it also allows to express requirements imposed on types by these elements to standardize
"existing practice".
<p/>
Considering details, it seems obvious that <i>Mandates:</i>, <i>Expects:</i> as well as <i>Requires:</i> are "suitable"
to be defined for types (With the acceptance of <a href="https://wg21.link/p1463r1">P1463R1</a> there are now also
<i>Mandates:</i> for types such as Table 65 &mdash; "Allocator-aware container requirements" for type <code>allocator_type</code>).
<p/>
For <i>Constraints:</i> the meaning would not be so clear: Should it mean that there is conditionally a type defined or not?
According to  the submitters knowledge there are currently no known examples for <i>Constraints:</i> to specify
constraint on types, therefore I'm suggesting to restrict this extension to <i>Mandates:</i>, <i>Expects:</i>,
and <i>Requires:</i> alone.
</p>

<p><i>[2019-03-15 Priority set to 3 after reflector discussion]</i></p>


<p><i>[2019-03-15; Daniel comments and provides wording]</i></p>

<p>
During the preparation of the wording for this issue it was found that we should allow
<i>Remarks:</i> elements to be used for other things than functions. One example of imposed
restrictions can be found in 17.12.3 <a href="https://wg21.link/cmp.common">[cmp.common]</a>:
</p>
<blockquote>
<pre>
template&lt;class... Ts&gt;
struct common_comparison_category {
  using type = <i>see below</i>;
};
</pre>
<p>
-2- <i>Remarks:</i> The member <i>typedef-name</i> <code>type</code> denotes the common comparison type
(11.10.3 <a href="https://wg21.link/class.spaceship">[class.spaceship]</a>) of <code>Ts...</code>, the expanded parameter pack. [&hellip;]
</p>
</blockquote>
<p>
The discussion of this issue speaks of "type" restrictions (versus the specified restrictions on
functions), because even the non-type template argument restrictions of 22.3.4 <a href="https://wg21.link/pair.astuple">[pair.astuple]</a>
appear in the context of a member type specification, but there are examples where not really a
single (member) type is involved, e.g. in the 22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a> example mentioned above.
<p/>
Another example is when such elements are used for the specification of template specializations, e.g.
in 22.4.7 <a href="https://wg21.link/tuple.helper">[tuple.helper]</a>:
</p>
<blockquote>
<pre>
template&lt;class T&gt; struct tuple_size;
</pre>
<blockquote>
<p>
-1- <i>Remarks:</i> All specializations of <code>tuple_size</code> shall satisfy the
<i>Cpp17UnaryTypeTrait</i> requirements (21.3.2 <a href="https://wg21.link/meta.rqmts">[meta.rqmts]</a>) with a base characteristic
of <code>integral_constant&lt;size_t, N&gt;</code> for some <code>N</code>.
</p>
</blockquote>
</blockquote>
<p>
Besides class template specializations, a second relevant use-case is the specification of member types
(Which are not necessarily part of a template), typically within the requirement tables, e.g. in
Table 62 &mdash; "Container requirements"'s entry <code>X::value_type</code>:
</p>
<blockquote>
<p>
<i>Requires:</i> <code>T</code> is <i>Cpp17Erasable</i> from <code>X</code>
</p>
</blockquote>
<p>
The suggested wording tries to cover the generalization by means of the term "non-function
entities" in addition to the existing functions to prevent being enforced to enumerate all entities
to which the extended rules apply.
</p>

<strong>Previous resolution [SUPERSEDED]:</strong>
<blockquote class="note">
<p>This wording is relative to <a href="https://wg21.link/n4810">N4810</a>.</p>

<ol>
<li><p>Change 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a>, as indicated:</p>

<blockquote>
<p>
-3- Descriptions of function semantics contain the following elements (as appropriate)<ins>; some
of these elements may also appear in the description of non-function entities as denoted
below</ins>: (footnote [&hellip;])
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; <i>Requires:</i> the preconditions <ins>imposed on a non-function entity,
or</ins> for calling the function.</p></li>
<li><p>(3.2) &mdash; <i>Constraints:</i> [&hellip;]</p></li>
<li><p>(3.3) &mdash; <i>Mandates:</i> the conditions that, if not met, render the program
ill-formed. [<i>Example:</i> An implementation might express such a condition
via the <i>constant-expression</i> in a <i>static_assert-declaration</i> (Clause 9).
If the diagnostic is to be emitted only after the function has been selected by overload resolution, an
implementation might express such a condition via a <i>constraint-expression</i>
(13.5.3 <a href="https://wg21.link/temp.constr.decl">[temp.constr.decl]</a>) and also define the function as deleted. &mdash; <i>end example</i>]</p></li>
<li><p>(3.4) &mdash; <i>Expects:</i> the conditions (sometimes termed preconditions) <ins>imposed
on a non-function entity, or</ins> that the function assumes to hold whenever
it is called. [<i>Example:</i> An implementation might express such conditions via an attribute such
as <code>[[expects]]</code> ( [dcl.attr.contract]) <ins>on a function declaration</ins>.
However, some such conditions might not lend themselves to expression via code. &mdash;
<i>end example</i>]</p></li> <li><p>[&hellip;]</p></li>
<li><p>(3.11) &mdash; <i>Remarks:</i> additional semantic constraints <del>on the function</del>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</p>
</blockquote>
</li>

<li><p>Change 99 [res.on.required], as indicated:</p>

<blockquote>
<p>
-1- Violation of any preconditions specified in a <del>function's</del> <i>Requires:</i> element
results in undefined behavior unless the function's <i>Throws:</i> element specifies
throwing an exception when <del>the</del><ins>a function's</ins> precondition is violated.
<p/>
-2- Violation of any preconditions specified in a<ins>n</ins> <del>function's</del> <i>Expects:</i>
element results in undefined behavior.
</p>
</blockquote>
</li>
</ol></blockquote>

<p><i>[2020-05-01; Daniel comments and adjusts wording to recent working draft]</i></p>

<p>
It should be pointed out that the originally referred to <i>Expects:</i> element has since
then be renamed to <i>Preconditions:</i> and that the <i>Requires:</i> element does now
only occur in annex D.
</p>

<p><i>[2020-06-11; Jonathan comments]</i></p>

<p>
This issue also affects some type traits such as <code>alignment_of</code>
and <code>make_signed</code>/<code>make_unsigned</code>.
In addition to clarifying what <i>Mandates:</i> means on a non-function
we need to decide exactly what is being mandated in the type traits.
Is instantiating the class template ill-formed,
or just odr-using the nested <code>type</code> or <code>value</code> member?
</p>


<p id="res-3193"><b>Proposed resolution:</b></p>
<p>This wording is relative to <a href="https://wg21.link/n4861">N4861</a>.</p>

<ol>
<li><p>Change 16.3.2.4 <a href="https://wg21.link/structure.specifications">[structure.specifications]</a>, as indicated:</p>

<blockquote>
<p>
-3- Descriptions of function semantics contain the following elements (as appropriate)<ins>; some
of these elements may also appear in the description of non-function entities as denoted
below</ins>: (footnote [&hellip;])
<ol style="list-style-type: none">
<li><p>(3.1) &mdash; <i>Constraints:</i> [&hellip;]</p></li>
<li><p>(3.2) &mdash; <i>Mandates:</i> the conditions that, if not met, render the program
ill-formed. [<i>Example:</i> An implementation might express such a condition
via the <i>constant-expression</i> in a <i>static_assert-declaration</i> (9.1 <a href="https://wg21.link/dcl.pre">[dcl.pre]</a>).
If the diagnostic is to be emitted only after the function has been selected by overload resolution, an
implementation might express such a condition via a <i>constraint-expression</i>
(13.5.3 <a href="https://wg21.link/temp.constr.decl">[temp.constr.decl]</a>) and also define the function as deleted. &mdash; <i>end example</i>]</p></li>
<li><p>(3.3) &mdash; <i>Preconditions:</i> the conditions <ins>imposed
on a non-function entity, or</ins> that the function assumes to hold whenever
it is called.</p></li> <li><p>[&hellip;]</p></li>
<li><p>(3.10) &mdash; <i>Remarks:</i> additional semantic constraints <del>on the function</del>.</p></li>
<li><p>[&hellip;]</p></li>
</ol>
</p>
</blockquote>
</li>

<li><p>Change  [res.on.expects], as indicated:</p>

<blockquote>
<p>
-1- Violation of any preconditions specified in a <del>function's</del> <i>Preconditions:</i>
element results in undefined behavior.
</p>
</blockquote>
</li>

<li><p>Change  [depr.res.on.required], as indicated:</p>

<blockquote class="note">
<p>
[<i>Drafting note:</i> Interestingly, albey the <i>Requires:</i> element has nearly vanished, the
issue is still relevant, see D.13 <a href="https://wg21.link/depr.meta.types">[depr.meta.types]</a>]
</p>
</blockquote>

<blockquote>
<p>
-1- Violation of any preconditions specified in a <del>function's</del> <i>Requires:</i> element
results in undefined behavior unless the function's <i>Throws:</i> element specifies
throwing an exception when <del>the</del><ins>a function's</ins> precondition is violated.
</p>
</blockquote>
</li>
</ol>




</body>
</html>
