<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2769: Redundant const in the return type of any_cast(const any&amp;)</title>
<meta property="og:title" content="Issue 2769: Redundant const in the return type of any_cast(const any&amp;)">
<meta property="og:description" content="C++ library issue. Status: C++17">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2769.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#C++17">C++17</a> status.</em></p>
<h3 id="2769"><a href="lwg-defects.html#2769">2769</a>. Redundant <code>const</code> in the return type of <code>any_cast(const any&amp;)</code></h3>
<p><b>Section:</b> 22.7.5 <a href="https://wg21.link/any.nonmembers">[any.nonmembers]</a> <b>Status:</b> <a href="lwg-active.html#C++17">C++17</a>
 <b>Submitter:</b> Casey Carter <b>Opened:</b> 2016-09-02 <b>Last modified:</b> 2017-07-30</p>
<p><b>Priority: </b>0
</p>
<p><b>View all other</b> <a href="lwg-index.html#any.nonmembers">issues</a> in [any.nonmembers].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#C++17">C++17</a> status.</p>
<p><b>Discussion:</b></p>
<p>
The overload of <code>any_cast</code> that accepts a reference to constant <code>any</code>:
</p>
<blockquote>
<pre>
template&lt;class ValueType&gt;
  ValueType any_cast(const any&amp; operand);
</pre>
</blockquote>
<p>
is specified to return <code>*any_cast&lt;add_const_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;(&amp;operand)</code> in [any.nonmembers]/5. This calls the pointer-to-constant overload of <code>any_cast</code>:
</p>
<blockquote>
<pre>
template&lt;class ValueType&gt;
  const ValueType* any_cast(const any* operand) noexcept;
</pre>
</blockquote>
<p>
which is specified as:
</p>
<blockquote>
<i>Returns:</i> If <code>operand != nullptr &amp;&amp; operand-&gt;type() == typeid(ValueType)</code>, a pointer to the object contained by <code>operand</code>; otherwise, <code>nullptr</code>.
</blockquote>
<p>
Since <code>typeid(T) == typeid(const T)</code> for all types <code>T</code>, <code>any_cast&lt;add_const_t&lt;T&gt;&gt;(&amp;operand)</code> is equivalent to <code>any_cast&lt;T&gt;(&amp;operand)</code> for all types <code>T</code> when operand is a constant lvalue <code>any</code>. 
The <code>add_const_t</code> in the return specification of the first overload above is therefore redundant.
</p>

<p><i>[2016-09-09 Issues Resolution Telecon]</i></p>

<p>P0; move to Tentatively Ready</p>
<p>Casey will provide combined wording for this and <a href="lwg-defects.html#2768" title="any_cast and move semantics (Status: C++17)">2768</a><sup><a href="https://cplusplus.github.io/LWG/issue2768" title="Latest snapshot">(i)</a></sup>, since they modify the same paragraph.</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Modify 22.7.5 <a href="https://wg21.link/any.nonmembers">[any.nonmembers]</a> as indicated:</p>
<blockquote>
<blockquote>
<pre>
template&lt;class ValueType&gt;
  ValueType any_cast(const any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp;&amp; operand);
</pre>
</blockquote>
<p>
-4- <i>Requires:</i> <code>is_reference_v&lt;ValueType&gt;</code> is <code>true</code> or 
<code>is_copy_constructible_v&lt;ValueType&gt;</code> is <code>true</code>. Otherwise the program is ill-formed.
<p/>
-5- <i>Returns:</i> <del>For the first form, 
<code>*any_cast&lt;add_const_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;(&amp;operand)</code>. 
For the second and third forms,</del> <code>*any_cast&lt;remove_reference_t&lt;ValueType&gt;&gt;(&amp;operand)</code>.
<p/>
[&hellip;]
</p>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-09-09 Casey improves wording as determined by telecon]</i></p>

<p>
The presented resolution is intended as the common wording for both LWG <a href="lwg-defects.html#2768" title="any_cast and move semantics (Status: C++17)">2768</a><sup><a href="https://cplusplus.github.io/LWG/issue2768" title="Latest snapshot">(i)</a></sup> and LWG <a href="lwg-defects.html#2769" title="Redundant const in the return type of any_cast(const any&amp;) (Status: C++17)">2769</a><sup><a href="https://cplusplus.github.io/LWG/issue2769" title="Latest snapshot">(i)</a></sup>.
</p>

<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Modify 22.7.5 <a href="https://wg21.link/any.nonmembers">[any.nonmembers]</a> as indicated:</p>
<blockquote>
<blockquote>
<pre>
template&lt;class ValueType&gt;
  ValueType any_cast(const any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp;&amp; operand);
</pre>
</blockquote>
<p>
-4- <i>Requires:</i> <code>is_reference_v&lt;ValueType&gt;</code> is <code>true</code> or 
<code>is_copy_constructible_v&lt;ValueType&gt;</code> is <code>true</code>. Otherwise the program is ill-formed.
<p/>
-5- <i>Returns:</i> For the first <del>form, 
<code>*any_cast&lt;add_const_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;(&amp;operand)</code>. 
For the</del> <ins>and</ins> second <del>and third</del> form<del>s</del>, 
<code>*any_cast&lt;remove_reference_t&lt;ValueType&gt;&gt;(&amp;operand)</code>. <ins>For the third form, 
<code>std::forward&lt;ValueType&gt;(*any_cast&lt;remove_reference_t&lt;ValueType&gt;&gt;(&amp;operand))</code>.</ins>
<p/>
[&hellip;]
</p>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-10-05, Tomasz and Casey reopen and improve the wording]</i></p>

<p>
The constraints placed on the non-pointer <code>any_cast</code> overloads are neither necessary nor sufficient to 
guarantee that the specified effects are well-formed. The current PR for LWG <a href="lwg-defects.html#2769" title="Redundant const in the return type of any_cast(const any&amp;) (Status: C++17)">2769</a><sup><a href="https://cplusplus.github.io/LWG/issue2769" title="Latest snapshot">(i)</a></sup> also makes it 
possible to retrieve a dangling lvalue reference to a temporary any with e.g. <code>any_cast&lt;int&amp;&gt;(any{42})</code>, 
which should be forbidden.
</p>

<p><i>[2016-10-16, Eric made some corrections to the wording]</i></p>


<p><strong>Previous resolution [SUPERSEDED]:</strong></p>
<blockquote class="note">
<p>This wording is relative to N4606.</p>

<ol>
<li><p>Modify 22.7.5 <a href="https://wg21.link/any.nonmembers">[any.nonmembers]</a> as indicated:</p>
<blockquote>
<blockquote>
<pre>
template&lt;class ValueType&gt;
  ValueType any_cast(const any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp;&amp; operand);
</pre>
</blockquote>
<p>
-4- <i>Requires:</i> <del><code>is_reference_v&lt;ValueType&gt;</code> is <code>true</code> or 
<code>is_copy_constructible_v&lt;ValueType&gt;</code> is <code>true</code>.</del><ins>For the first 
overload, <code>is_constructible_v&lt;ValueType, const remove_cv_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&amp;&gt;</code> 
is <code>true</code>. For the second overload, <code>is_constructible_v&lt;ValueType, 
remove_cv_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&amp;&gt;</code> is <code>true</code>. For the third overload, 
<code>is_constructible_v&lt;ValueType, remove_cv_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;</code> is 
<code>true</code>.</ins> Otherwise the program is ill-formed.
<p/>
-5- <i>Returns:</i> <del>For the first form, 
<code>*any_cast&lt;add_const_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;(&amp;operand)</code>. For the second and third 
forms, <code>*any_cast&lt;remove_reference_t&lt;ValueType&gt;&gt;(&amp;operand)</code>.</del><ins>For the first and second 
overload, <code>static_cast&lt;ValueType&gt;(*any_cast&lt;remove_cv_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;(&amp;operand))</code>. For the third overload, <code>static_cast&lt;ValueType&gt;(std::move(*any_cast&lt;remove_cv_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;(&amp;operand)))</code>.</ins>
<p/>
[&hellip;]
</p>
</blockquote>
</li>
</ol>
</blockquote>

<p><i>[2016-11-10, LWG asks for simplification of the wording]</i></p>

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

<p>Move to Tentatively Ready</p>


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

<ol>
<li><p>Modify 22.7.5 <a href="https://wg21.link/any.nonmembers">[any.nonmembers]</a> as indicated:</p>
<blockquote>
<blockquote>
<pre>
template&lt;class ValueType&gt;
  ValueType any_cast(const any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp; operand);
template&lt;class ValueType&gt;
  ValueType any_cast(any&amp;&amp; operand);
</pre>
</blockquote>
<p>
<ins>-?- Let <code>U</code> be the type <code>remove_cv_t&lt;remove_reference_t&lt;ValueType&gt;&gt;</code>.</ins>
</p>
<p>
-4- <i>Requires:</i> <del><code>is_reference_v&lt;ValueType&gt;</code> is <code>true</code> or
<code>is_copy_constructible_v&lt;ValueType&gt;</code> is <code>true</code>.</del><ins>For the first
overload, <code>is_constructible_v&lt;ValueType, const U&amp;&gt;</code>
is <code>true</code>. For the second overload, <code>is_constructible_v&lt;ValueType,
U&amp;&gt;</code> is <code>true</code>. For the third overload,
<code>is_constructible_v&lt;ValueType, U&gt;</code> is
<code>true</code>.</ins> Otherwise the program is ill-formed.
<p/>
-5- <i>Returns:</i> <del>For the first form,
<code>*any_cast&lt;add_const_t&lt;remove_reference_t&lt;ValueType&gt;&gt;&gt;(&amp;operand)</code>. For the second and third
forms, <code>*any_cast&lt;remove_reference_t&lt;ValueType&gt;&gt;(&amp;operand)</code>.</del><ins>For the first and second
overload, <code>static_cast&lt;ValueType&gt;(*any_cast&lt;U&gt;(&amp;operand))</code>. For the third overload, <code>static_cast&lt;ValueType&gt;(std::move(*any_cast&lt;U&gt;(&amp;operand)))</code>.</ins>
<p/>
[&hellip;]
</p>
</blockquote>
</li>
</ol>






</body>
</html>
