<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/118560>118560</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [libc++] Cannot call `std::visit` with immediate lambda visitor
        </td>
    </tr>

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

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          LHLaurini
      </td>
    </tr>
</table>

<pre>
    This code
```c++
#include <variant>

consteval auto func(const std::variant<int>& value) {
    return std::visit([](auto val) consteval { return val; }, value);
}
```

Compiles with both GCC and Clang + stdlibc++. However, it fails to compile with Clang + libc++.

```
In file included from <source>:2:
/opt/compiler-explorer/clang-trunk-20241203/bin/../include/c++/v1/variant:497:20: error: constexpr variable '__fmatrix' must be initialized by a constant expression
  497 |     constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
      | ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-trunk-20241203/bin/../include/c++/v1/variant:583:20: note: in instantiation of function template specialization 'std::__variant_detail::__visitation::__base::__visit_alt<std::__variant_detail::__visitation::__variant::__value_visitor<(lambda at <source>:5:23)>, const std::__variant_detail::__impl<int> &>' requested here
  583 |     return __base::__visit_alt(
      | ^
/opt/compiler-explorer/clang-trunk-20241203/bin/../include/c++/v1/variant:595:12: note: in instantiation of function template specialization 'std::__variant_detail::__visitation::__variant::__visit_alt<std::__variant_detail::__visitation::__variant::__value_visitor<(lambda at <source>:5:23)>, const std::variant<int> &>' requested here
  595 | return __visit_alt(__make_value_visitor(std::forward<_Visitor>(__visitor)), std::forward<_Vs>(__vs)...);
      | ^
/opt/compiler-explorer/clang-trunk-20241203/bin/../include/c++/v1/variant:1566:21: note: in instantiation of function template specialization 'std::__variant_detail::__visitation::__variant::__visit_value<(lambda at <source>:5:23), const std::variant<int> &>' requested here
 1566 |   return __variant::__visit_value(std::forward<_Visitor>(__visitor), std::forward<_Vs>(__vs)...);
      |                     ^
<source>:5:17: note: in instantiation of function template specialization 'std::visit<(lambda at <source>:5:23), const std::variant<int> &, void>' requested here
    5 |     return std::visit([](auto val) consteval { return val; }, value);
      | ^
/opt/compiler-explorer/clang-trunk-20241203/bin/../include/c++/v1/variant:497:20: note: pointer to a consteval declaration is not a constant expression
  497 |     constexpr auto __fmatrix = __make_fmatrix<_Visitor&&, decltype(std::forward<_Vs>(__vs).__as_base())...>();
      | ^
/opt/compiler-explorer/clang-trunk-20241203/bin/../include/c++/v1/variant:529:59: note: declared here
  529 |     _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __dispatch(_Fp __f, _Vs... __vs) {
      | ^
1 error generated.
Compiler returned: 1
```

[Demo on Compiler Explorer](https://godbolt.org/z/5TbfGoo8v). Interestingly, MSVC also has trouble with this example (read outside lifetime, for some reason).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcV1GPm0gS_jXtl9IgaIyBBz_YeEhGSnTRXZRX1EDZ7ktDc92NM5OH--2rAmwTJ6PNbrKz0Vpo7IGqrqrv-7roEtbKQ4u4ZtGWRbuF6N1Rm_Wb129Eb2QrF6Wun9bvj9JCpWtk_oat_PGqGN_S5W8YD2Vbqb5GYGF2EkaK1rHwnh75m0q31uFJKBC907Dv24rxZLgL1tUs3LBwc3HK5OjKV3ASqkfGU2AxRQEAMOh6087cpJWO8WTMnvFkCHESityugVm8PbvSs3ALLN4xnl1CsHCoI97NCxzTz3TTSYUWPkl3hFK7I7zKMhBtDZkS7QEY31JCSpYTIh681p_whIYiSAd7IZUFp6EalxpXujrPPMeQ8wweWtiTzwRwDXujG4LZ6t5USFCFG05oEA-57hzj-RTI3OFjp7ShTPKK4t0507cf77jPlwH3Q8bzUraM557HeD6FINuJWp6fAvpzJmez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