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

    <tr>
        <th>Summary</th>
        <td>
            [libcxx] std::bind : The args in checking does not match the actual args when calling the functor
        </td>
    </tr>

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

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

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

<pre>
    In __bind::operator()(), the functor args in __bind_return is _Fd, but in __apply_functor, the actual functor args is _Fd&
```cpp
template<class _Fp, class ..._BoundArgs>
class __bind
    : public __weak_result_type<typename decay<_Fp>::type>
{
...
template <class ..._Args>
    typename __bind_return<_Fd, _Td, tuple<_Args&&...> >::type
    operator()(_Args&& ...__args)
    {
        return _VSTD::__apply_functor(__f_, __bound_args_, __indices(),
                              tuple<_Args&&...>(_VSTD::forward<_Args>(__args)...));
    }
...
};

template <class _Fp, class _BoundArgs, size_t ..._Indx, class _Args>
inline _LIBCPP_INLINE_VISIBILITY
typename __bind_return<_Fp, _BoundArgs, _Args>::type
__apply_functor(_Fp& __f, _BoundArgs& __bound_args, __tuple_indices<_Indx...>,
                _Args&& __args)
{
    return _VSTD::__invoke(__f, _VSTD::__mu(_VSTD::get<_Indx>(__bound_args), __args)...);
}
```

The following sample code will report error when use clang with libcxx, but works normally with gcc and libstdc++

```cpp
#include <functional>

struct some_class {
    void operator()(int) && {

    }
    void operator()() & {

    }
};

int main()
{
    some_class obj;
    auto tmp = std::bind(obj);
    tmp();
}

// clang++ tmp.cpp -o main -stdlib=libc++
// report error:
/*
tmp.cpp:16:5: error: no matching function for call to object of type '__bind<some_class &>' 
    tmp(); 
    ^~~ 
/usr/bin/../include/c++/v1/__functional/bind.h:293:9: note: candidate template ignored: substitution failure [with _Args = <>]: i
mplicit instantiation of undefined template 'std::__bind_return<some_class, std::tuple<>, std::tuple<>, false>' 
        operator()(_Args&& ...__args) 
        ^ 
/usr/bin/../include/c++/v1/__functional/bind.h:302:9: note: candidate template ignored: substitution failure [with _Args = <>]: i
mplicit instantiation of undefined template 'std::__bind_return<const some_class, const std::tuple<>, std::tuple<>, false>' 
        operator()(_Args&& ...__args) const 
        ^ 
1 error generated.
*/

// g++ tmp.cpp -o main // works normally
```
This maybe the functor arg in __bind_return is _Fd, and in the final ` _VSTD::__invoke(_VSTD::declval<_XFp>(), _VSTD::declval<_XArgs>()...));` will use declval<_Fd>(), which is _Fd&&, does not match the type in __apply_functor, which is _Fd&
```cpp
template <class _Fp, class _BoundArgs, class _TupleUj,
          bool = __is_valid_bind_return<_Fp, _BoundArgs, _TupleUj>::value>
struct __bind_return;

template <class _Fp, class ..._BoundArgs, class _TupleUj>
struct __is_valid_bind_return<_Fp, tuple<_BoundArgs...>, _TupleUj>
{
    static const bool value = __invokable<_Fp,
                    typename __mu_return<_BoundArgs, _TupleUj>::type...>::value;
};

template <class _Fp, class ..._Args>
using __invokable = __invokable_r<void, _Fp, _Args...>;

template <class _Ret, class _Fp, class ..._Args>
struct __invokable_r
{
  static const bool value = type::value;
  using type =
  typename conditional<
      _IsNotSame<_Result, __nat>::value,
      typename conditional<
          is_void<_Ret>::value,
          true_type,
          is_convertible<_Result, _Ret>
      >::type,
      false_type
  >::type;

  using _Result = decltype(__try_call<_Fp, _Args...>(0));

  template <class _XFp, class ..._XArgs>
  static auto __try_call(int) -> decltype(
    _VSTD::__invoke(_VSTD::declval<_XFp>(), _VSTD::declval<_XArgs>()...));
  template <class _XFp, class ..._XArgs>
  static __nat __try_call(...);
};
```


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWFuP4jgW_jXmxSqUciAUDzwABRJSqzWarh317ovlOAY87diR7UDXPsxvX9lOgpNATffualeDEBfH5_odn4uJMfwkGVuB-QbMXyektmelV_9gWklWidpMclW8rw4SYpxzWYB0DdK1qpgmVmmAXgBatp9baM8MHmtJrdKQ6JO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