<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - std::visit fails to compile in a specific case with const in visitor return type"
   href="https://bugs.llvm.org/show_bug.cgi?id=47780">47780</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>std::visit fails to compile in a specific case with const in visitor return type
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>libc++
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>11.0
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>Windows NT
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>All Bugs
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>predelnik@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, mclow.lists@gmail.com
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following program:

#include <variant>

class X
{
};

class PseudoIt
{
 public:
   X operator* () { return X{};}
};

int main ()
{
    std::variant<PseudoIt, X*> v;
    // commenting out const below VVV fixes the error
    std::visit ([](auto &&x) -> const X { return *x; }, v);
}

fails to compile with current trunk libc++. Here's only the final bit of
obviously long compilation stack:

/opt/compiler-explorer/clang-trunk-20201009/bin/../include/c++/v1/variant:502:12:
error: address of overloaded function '__dispatch' does not match required type
'const X
(std::__1::__variant_detail::__visitation::__variant::__value_visitor<(lambda
at <source>:17:17)> &&,
std::__1::__variant_detail::__base<std::__1::__variant_detail::_Trait::_TriviallyAvailable,
PseudoIt, X *> &)'

    return __dispatcher<_Is...>::template __dispatch<_Fp, _Vs...>;

Removing const in lambda return type fixes the compilation. Other variant
implementations work fine in such case.

godbolt link for convenience:
<a href="https://gcc.godbolt.org/z/5q4s1d">https://gcc.godbolt.org/z/5q4s1d</a>

I've got this const in some template code and it was not necessary in any way
but still I think code like this should compile.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>