jeudi 23 avril 2020

Why std::is_invocable

std::is_invocable<std::decay_t<void(int&)>, std::decay_t<int>>::value

evaluates to false.

But

void(int&) decays to void*(int&)

and int to int

and I can use std::invoke like this:

void f(int&);
...
auto* fp = f;
int i = 0;
std::invoke(fp, i);

I bumped into this when I was looking at std::thread constructor:

 template<typename _Callable, typename... _Args,
             typename = _Require<__not_same<_Callable>>>
      explicit
      thread(_Callable&& __f, _Args&&... __args)
      {
        static_assert( __is_invocable<typename decay<_Callable>::type,
                                      typename decay<_Args>::type...>::value,
          "std::thread arguments must be invocable after conversion to rvalues"
          );

I understand why passing references to std::thread's constructor is disouraged (we still can use std::ref()) but I cannot understand why void *(int&) cannot be invoked with int.

Aucun commentaire:

Enregistrer un commentaire