fork download
  1. #include <functional>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. struct MyType{
  6. static int a;
  7. int m_a;
  8. MyType(){a++; m_a = a;}
  9.  
  10. void print(){
  11. std::cout << "MyType: " << a << std::endl;
  12. }
  13. };
  14. int MyType::a=0;
  15.  
  16.  
  17. struct Functor{
  18.  
  19. static int a;
  20. int m_a;
  21.  
  22. Functor(){a++; m_a = a;}
  23.  
  24. // Binding this function does not work
  25. template<typename T>
  26. void calculate(T * t){}
  27.  
  28. // Binding this function works!!!
  29. void calculate2(MyType * t){
  30. std::cout << " Functor: " << m_a <<std::endl;
  31. t->print();
  32. }
  33.  
  34. };
  35.  
  36. int Functor::a=0;
  37.  
  38. // Binding this function works!!!
  39. template<typename T>
  40. void foo( T * t){}
  41.  
  42. class ExecutionList{
  43. public:
  44. typedef MyType Type;
  45.  
  46. template<typename T>
  47. void addFunctor(T * extForce){
  48. m_calculationList.push_back( std::bind(T::calculate<Type>, extForce, std::placeholders::_1 ) );
  49. m_calculationList.push_back( std::bind(&T::calculate2, extForce, std::placeholders::_1 ) );
  50. m_calculationList.push_back( std::bind(&foo<Type>, std::placeholders::_1 ) );
  51. }
  52.  
  53.  
  54. void calculate(Type * t){
  55. for(auto it = m_calculationList.begin(); it != m_calculationList.end();it++){
  56. (*it)(t); // Apply calculation function!
  57. }
  58. }
  59.  
  60. private:
  61. std::vector< std::function<void (Type *)> > m_calculationList;
  62. };
  63.  
  64.  
  65.  
  66. int main(){
  67. MyType b;
  68. ExecutionList list;
  69. list.addFunctor(new Functor());
  70. list.addFunctor(new Functor());
  71. list.calculate(&b);
  72. }
Compilation error #stdin compilation error #stdout 0s 3476KB
stdin
Standard input is empty
compilation info
prog.cpp: In member function ‘void ExecutionList::addFunctor(T*)’:
prog.cpp:48:73: error: expected primary-expression before ‘>’ token
                 m_calculationList.push_back( std::bind(T::calculate<Type>, extForce, std::placeholders::_1 ) );
                                                                         ^
prog.cpp:48:74: error: expected primary-expression before ‘,’ token
                 m_calculationList.push_back( std::bind(T::calculate<Type>, extForce, std::placeholders::_1 ) );
                                                                          ^
stdout
Standard output is empty