fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<class FUNC>
  5. class IndexFunctor
  6. {
  7. public:
  8. typedef FUNC FUNC_T;
  9.  
  10. explicit IndexFunctor(const FUNC_T& func) : func(func), index(0) {}
  11.  
  12. template<typename... Args>
  13. auto operator ()(Args&&... args)
  14. -> decltype(func(std::forward<Args>(args)..., index++)) //get return type
  15. {
  16. return func(std::forward<Args>(args)..., index++);
  17. }
  18.  
  19. const FUNC_T& GetFunctor() const
  20. {
  21. return func;
  22. }
  23.  
  24. int GetIndex() const
  25. {
  26. return index;
  27. }
  28.  
  29. void SetIndex(int index)
  30. {
  31. this->index = index;
  32. }
  33.  
  34. private:
  35. FUNC_T func;
  36. int index;
  37. };
  38.  
  39. template<class FUNC>
  40. IndexFunctor<FUNC> with_index(const FUNC& func)
  41. {
  42. return IndexFunctor<FUNC>(func);
  43. }
  44.  
  45. int main()
  46. {
  47. auto f = with_index([](int a, int index){ return a * index; });
  48. int a = f(5);
  49. cout << a << endl;
  50. }
  51.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
1
2
10
42
11
compilation info
prog.cpp:14:48: error: ‘index’ was not declared in this scope
  -> decltype(func(std::forward<Args>(args)..., index++)) //get return type
                                                ^
prog.cpp:14:48: error: ‘index’ was not declared in this scope
prog.cpp: In function ‘int main()’:
prog.cpp:48:16: error: no match for call to ‘(IndexFunctor<main()::__lambda0>) (int)’
     int a = f(5);
                ^
prog.cpp:5:7: note: candidate is:
 class IndexFunctor
       ^
prog.cpp:13:7: note: template<class ... Args> decltype (func((forward<Args>)(IndexFunctor::operator()::args)..., <expression error>)) IndexFunctor<FUNC>::operator()(Args&& ...) [with Args = {Args ...}; FUNC = main()::__lambda0]
  auto operator ()(Args&&... args) 
       ^
prog.cpp:13:7: note:   template argument deduction/substitution failed:
prog.cpp: In substitution of ‘template<class ... Args> decltype (func((forward<Args>)(IndexFunctor::operator()::args)..., <expression error>)) IndexFunctor<FUNC>::operator()(Args&& ...) [with Args = {Args ...}; FUNC = main()::__lambda0] [with Args = {int}]’:
prog.cpp:48:16:   required from here
prog.cpp:14:55: error: ‘func’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
  -> decltype(func(std::forward<Args>(args)..., index++)) //get return type
                                                       ^
prog.cpp:14:55: note: declarations in dependent base ‘IndexFunctor<main()::__lambda0>’ are not found by unqualified lookup
prog.cpp:14:55: note: use ‘IndexFunctor::func’ instead
stdout
Standard output is empty