fork download
  1. #include <iostream>
  2. #include <array>
  3. #include <type_traits>
  4.  
  5. template<typename F>
  6. struct length;
  7.  
  8. template<typename F , std::size_t N = length<F>::value>
  9. class Algorithm
  10. {
  11. public:
  12. Algorithm( F f )
  13. {}
  14.  
  15. void execute();
  16.  
  17. private:
  18. F _f;
  19. };
  20.  
  21. template<typename F , std::size_t N>
  22. void Algorithm<F,N>::execute()
  23. {
  24. _f();
  25. }
  26.  
  27. struct Curve
  28. {
  29. Curve() = default;
  30. Curve( const Curve& ) = default;
  31.  
  32. std::array<double,3> operator()() const;
  33. };
  34.  
  35. std::array<double,3> Curve::operator()() const
  36. {
  37. return {{ 1.0f , 2.0f , 3.0f }};
  38. }
  39.  
  40. template<>
  41. struct length<Curve> : public std::integral_constant<std::size_t,3>
  42. {};
  43.  
  44. template<typename F>
  45. Algorithm<F> make_algorithm( F f )
  46. {
  47. return { f };
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53. Algorithm<Curve> alg{ Curve{} }; //C++98/03 style
  54. auto alg2 = make_algorithm( Curve{} ); //C++11 style
  55. }
  56.  
  57.  
Success #stdin #stdout 0s 3336KB
stdin
Standard input is empty
stdout
Standard output is empty