fork download
  1. #include <functional>
  2. #include <iostream>
  3.  
  4. template<typename T>
  5. struct mystruct
  6. {
  7. T variable;
  8. };
  9.  
  10. int myfunc(int x)
  11. {
  12. return 2*x;
  13. }
  14.  
  15. template<typename T>
  16. int calculate(
  17. mystruct<T> custom_struct,
  18. std::function<T(T)> custom_func)
  19. {
  20. return custom_func(custom_struct.variable);
  21. }
  22.  
  23. int main()
  24. {
  25. mystruct<int> A;
  26. A.variable=6;
  27. std::cout<<calculate<int>(A,myfunc)<<std::endl; // error here!!!
  28. return 0;
  29. }
  30.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
12