C ++ Understanding the Complex Signature Function

While reading some code, I came across this function. I have many problems understanding the function signature. What things do I need to know before I can make the head or tail of the following code? I have been using C ++ for some time now. I know what patterns, function pointers. However, I cannot figure out what might mean T::*that a line starting with _Defermeans semantically. Also, the first line of the function seems pretty intimidating. Is there some kind of resource that I can read before trying to overestimate this code?

template <typename T>
_Defer<void(*(PID<T>, void (T::*)(void)))
       (const PID<T>&, void (T::*)(void))>
defer(const PID<T>& pid, void (T::*method)(void))
{
  void (*dispatch)(const PID<T>&, void (T::*)(void)) =
    &process::template dispatch<T>;
  return std::tr1::bind(dispatch, pid, method);
}

Source: https://github.com/3rdparty/libprocess/blob/master/include/process/defer.hpp

+4
source share
2

:

template<typename T>
using VoidPMemberFn = void(T::*)(); // Pointer to member function of T
                                    // that accepts no arguments and
                                    // returns nothing.

template<typename T>
using DeferResultType = void (*)(const PID<T> &, VoidPMemberFn<T>);

template<typename T>
using DeferSignatureType = DeferResultType<T>(PID<T>, VoidPMemberFn<T>);

template<typename T>
_Defer<DeferSignatureType<T>> defer(const PID<T> &pid, VoidPMemberFn<T> method)
{
    // Stuff...
}

, _Defer :

void(* (PID<T>, void (T::*)(void)) )(const PID<T>&, void (T::*)(void))
^-+^-^ ^-------------------------^  ^-------------+------------------^
  |           argument list                       |
  |                                               |
  +-----------------------------------------------+
              return type:  void(*)(const PID<T> &, void(T::*)(void))

"", , std::function (, std::function<int(float)>).

:

using sig = int(float);

sig gn; // Same as: "int gn(float)", a function declaration

int fn(float x)
{return (int)x;}

int main(int argc, char **argv)
{
    // pointer to a function with signature "sig"
    sig *pfn = &fn;
    return 0;
}

int gn(float x)
{return (int)(x*x);}
+5

, , .

_Defer<void(*(PID<T>, void (T::*)(void)))(const PID<T>&, void (T::*)(void))>

_Defer, . .

_Defer<> std, , , . std::tr1::bind std::bind.

, ++ 14

template <typename T>
auto defer(const PID<T>& pid, void (T::*method)())
{
  auto dispatch = &process::template dispatch<T>;
  return std::bind(dispatch, pid, method);
}

process ( , defer -) -- dispatch. , defer() (. std::bind), .

, std::bind , , auto ( std::function) .

0

All Articles