lundi 28 décembre 2015

getting the type of operator() from a bind versus a lambda function

Please take a look at the following code below. In the case of a capture lambda expression it will work and compile just fine. Although when using a bind callable expression I will get an overload expression expression error

main.cpp:13:60: error: decltype cannot resolve address of overloaded function 
 using FuncType = decltype(&std::decay<T>::type::operator()); 

How can I code this such that I am getting the type of the operator() method for the bind function? The bind type is std::_Bind<void (*(std::_Placeholder<2>, std::_Placeholder<1>))(int, int)> which I also don't fully understand; I know void (*) (int, int) as a type as a pointer to function taking in int, int and returning void. I guess I don't understand the syntax void *(x,y) (int,int); basically the x,y part. I was assuming that after all the template stuff there is a method void operator()(int, int) that will get resolved that bind_f(x,y) will call and I was trying to capture that type.

#include <iostream>
#include <functional>
#include <type_traits>

void tester(int x, int y) {
   std::cout << " x = " << x << " y = " << y << std::endl;
}

template <typename T>
class TypeChecker;

template <typename T>
using FuncType = decltype(&std::decay<T>::type::operator());

int main()  {
   using namespace std::placeholders;
   auto bind_f = std::bind(tester, _2, _1);
   bind_f(1,2);                            

   int y = 5;                              

   auto lambda = [y]() {                   
      std::cout << " y = " << y << std::endl;
   };                                        

   typedef FuncType<decltype(lambda)> x1;            
   typedef FuncType<decltype(bind_f)> x2;            

   //TypeChecker<decltype(bind_f)> t2;       

}                                            

Aucun commentaire:

Enregistrer un commentaire