fork download
  1. template<class T, template<class> class Functor>
  2. class FunctorInterface_1 {
  3. private:
  4. const Functor<T> &f_cref;
  5. public:
  6. FunctorInterface_1() : f_cref(static_cast<const Functor<T>&>(*this)) {}
  7. T operator() ( T val ) const { return f_cref(val); }
  8. }; // FunctorInterface_1 (works)
  9.  
  10. // Declare a traits class.
  11. template <typename T> struct FunctorTraits;
  12.  
  13. template<class Functor>
  14. class FunctorInterface_2 {
  15. private:
  16. const Functor &f_cref;
  17. public:
  18.  
  19. // Use the traits class to define Ftype
  20. using Ftype = typename FunctorTraits<Functor>::type;
  21.  
  22. FunctorInterface_2() : f_cref(static_cast<const Functor&>(*this)) {}
  23. Ftype operator() ( Ftype val ) const { return f_cref(val); }
  24. }; // FunctorInterface_2 (no type in Functor!)
  25.  
  26. // Forward declare Cube to specialize FunctorTraits
  27. template<class T> struct Cube;
  28.  
  29. // Specialize FunctorTraits for Cube
  30. template <typename T> struct FunctorTraits<Cube<T>>
  31. {
  32. using type = T;
  33. };
  34.  
  35. template<class T>
  36. struct Cube : public FunctorInterface_2<Cube<T>> {
  37. using type = T;
  38. T operator() ( T val ) const { return val*val*val; }
  39. }; // Cube
  40.  
  41. int main()
  42. {
  43. Cube<int> a;
  44. }
  45.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty