fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. enum typeData
  7. {
  8. ValueDouble,
  9. ValueFloat
  10. };
  11.  
  12. struct data
  13. {
  14. typeData TypeData;
  15. vector<double> getDoubleValues() const { return vector<double>(); }
  16. vector<float> getFloatValues() const { return vector<float>(); }
  17. };
  18.  
  19. template <typename R, typename F>
  20. R Dispatch(const data& d, F f)
  21. {
  22. switch (d.TypeData)
  23. {
  24. case ValueDouble: return f(d.getDoubleValues());
  25. case ValueFloat: return f(d.getFloatValues());
  26. default: break; // ?
  27. }
  28. }
  29.  
  30. //usage
  31. int func(const std::vector<double>& v)
  32. {
  33. return v.size();
  34. }
  35.  
  36. int func(const std::vector<float>& v)
  37. {
  38. return v.size() + 1;
  39. }
  40.  
  41. template <typename T>
  42. struct D
  43. {
  44. template <typename V>
  45. T operator()(const std::vector<V>& v) const { return func(v); }
  46. };
  47.  
  48. int main()
  49. {
  50. data d;
  51. d.TypeData = ValueDouble;
  52.  
  53. int h1 = Dispatch<int>(d, D<int>());
  54. cout << "h1 = " << h1 << endl;
  55.  
  56. d.TypeData = ValueFloat;
  57. int h2 = Dispatch<int>(d, D<int>());
  58. cout << "h2 = " << h2 << endl;
  59. return 0;
  60. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
h1 = 0
h2 = 1