fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. enum DataType {
  7. Int,
  8. Float,
  9. String,
  10. };
  11. struct Info {
  12. DataType typ;
  13. };
  14. struct Data {
  15. template<typename T> T read(Info i)const { return T(); }
  16. };
  17.  
  18. void write(int) { cout << "INT" << endl; }
  19. void write(float) { cout << "FLOAT" << endl; }
  20. void write(string) { cout << "STRING" << endl; }
  21.  
  22. template<class T> void perform(const Info& i, const Data& d){
  23. T value = d.read<T>(i);
  24. write(value);
  25. }
  26.  
  27. template<int N, class Head, class... Tail>
  28. struct Dispatch {
  29. static void dispatch(const Info& i, const Data& d) {
  30. if (i.typ == N)
  31. perform<Head>(i, d);
  32. else
  33. Dispatch<N+1, Tail...>::dispatch(i, d);
  34. }
  35. };
  36.  
  37. template<int N, class Last>
  38. struct Dispatch<N, Last> {
  39. static void dispatch(const Info& i, const Data& d) {
  40. if (i.typ == N)
  41. perform<Last>(i, d);
  42. }
  43. };
  44.  
  45. void run(const Info& i, const Data& d) {
  46. Dispatch<0, int, float, string>::dispatch(i, d);
  47. }
  48.  
  49. int main() {
  50. Data d1;
  51. Info i1{ Float };
  52. run(i1, d1);
  53. Info i2{ String };
  54. run(i2, d1);
  55. }
Success #stdin #stdout 0s 4484KB
stdin
Standard input is empty
stdout
FLOAT
STRING