fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. using namespace std;
  5.  
  6. typedef double R;
  7.  
  8. auto f( std::array<R, 3> &p ) -> R {
  9. return p[ 0 ] + 2 * p[ 1 ] + 3 * p[ 2 ];
  10. };
  11.  
  12. class Ftor
  13. {
  14. int idx;
  15. R (*func)( std::array<R, 3> &p );
  16. public:
  17. Ftor( int _idx, R (*_func)( std::array<R, 3> &p ) ): idx( _idx ), func( _func ) {};
  18. R operator()( R x )
  19. {
  20. std::array<R, 3> p = { 0, 0, 0 };
  21. p[ idx ] = x;
  22. return func( p );
  23. }
  24. };
  25.  
  26. auto main() -> int {
  27. Ftor fx{ 0, f };
  28. Ftor fy{ 1, f };
  29. Ftor fz{ 2, f };
  30.  
  31. cout << fx( 5 ) << endl; // 5
  32. cout << fy( 5 ) << endl; // 10
  33. cout << fz( 5 ) << endl; // 15
  34.  
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
5
10
15