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

    <tr>
        <th>Summary</th>
        <td>
            Regression libc++: Usage of std::ref compiles in C++17 but not C++20
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    Tested with Clang 16.0.0.

Minimal example:

```cpp
//#undef __cplusplus
//#define __cplusplus 201703 // compiles when forcing libc++ as if compiling under C++17
#include <functional>
#include <iostream>
#include <type_traits>

template<class Fun>
class y_combinator_result {
 Fun fun_;
public:
    template<class T>
    explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

 template<class ...Args>
    decltype(auto) operator()(Args &&...args) {
        return fun_(std::ref(*this), std::forward<Args>(args)...);
 }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
    return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

int main() 
{
    using Type = int; // Using an other integer type such as "unsigned int" makes the code compile
    
    auto almost_factorial = [](auto factorial, Type n) -> Type {
        if (n == 0) {
 return static_cast<Type>(1); 
        } else {
            return static_cast<Type>(n * factorial(n-1));
        }
    };
    auto factorial = y_combinator(std::ref(almost_factorial)); // Not using std::ref makes the code compile
    
    //std::cout << factorial(5u) << std::endl; // Uncommenting this line makes the code compile
    std::cout << factorial(5) << std::endl; // Does not compile without previous line
 
    return 0;
}
```

Compilation output on [cpp.sh](https://cpp.sh/?source=%23include+%3Ciostream%3E%0A%23include+%3Ctype_traits%3E%0Ausing+namespace+std%3B%0A%0Aint+main()+%0A%7B%0A++++std%3A%3Acout+%3C%3C+083+%3C%3C+std%3A%3Aendl%3B%0A++++%0A++++return+0%3B%0A%7D):

```
main.cpp:33:42: error: no matching function for call to object of type 'std::reference_wrapper<y_combinator_result<std::reference_wrapper<(lambda at main.cpp:29:29)>>>'
            return static_cast<Type>(n * factorial(n-1));
 ^~~~~~~~~
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/type_traits:3640:23: note: in instantiation of function template specialization 'main()::(anonymous class)::operator()<std::reference_wrapper<y_combinator_result<std::reference_wrapper<(lambda at main.cpp:29:29)>>>>' requested here
    { return static_cast<_Fp&&>(__f)(static_cast<_Args&&>(__args)...); }
 ^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__functional/reference_wrapper.h:60:23: note: in instantiation of function template specialization 'std::__invoke<(lambda at main.cpp:29:29) &, std::reference_wrapper<y_combinator_result<std::reference_wrapper<(lambda at main.cpp:29:29)>>>, int>' requested here
        return _VSTD::__invoke(get(), _VSTD::forward<_ArgTypes>(__args)...);
 ^
main.cpp:16:16: note: in instantiation of function template specialization 'std::reference_wrapper<(lambda at main.cpp:29:29)>::operator()<std::reference_wrapper<y_combinator_result<std::reference_wrapper<(lambda at main.cpp:29:29)>>>, int>' requested here
        return fun_(std::ref(*this), std::forward<Args>(args)...);
 ^
main.cpp:39:27: note: in instantiation of function template specialization 'y_combinator_result<std::reference_wrapper<(lambda at main.cpp:29:29)>>::operator()<int>' requested here
    std::cout << factorial(5) << std::endl; // Does not compile without previous line
 ^
/root/emsdk/upstream/emscripten/cache/sysroot/include/c++/v1/__functional/reference_wrapper.h:59:5: note: candidate template ignored: substitution failure [with _ArgTypes = <int>]: no type named 'type' in 'std::__invoke_of<y_combinator_result<std::reference_wrapper<(lambda at main.cpp:29:29)>> &, int>'
    operator() (_ArgTypes&&... __args) const {
    ^
1 error generated.
```

maybe related to #43743
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMWEuT4roV_jVioxqXkcB2L1jwaHZJpW76ZusS8gGUsSVHkruHLPLbU0eyjaGZ23Mr0zWhGA8tHZ3Xd16ycE6dNMCKLDdkuZuJzp-NXf1NfYP6NyW_zg6muqxewHmo6JvyZ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