fork download
  1. #include <stdio.h>
  2. #include <vector>
  3. using namespace std;
  4. struct Space2
  5. {
  6. struct Vector
  7. {
  8.  
  9. };
  10.  
  11. };
  12. struct Space3
  13. {
  14. struct Vector
  15. {
  16.  
  17. };
  18.  
  19. };
  20.  
  21. template<typename Space>
  22. struct ParticleData{};
  23.  
  24. template <>
  25. struct ParticleData<Space2>
  26. {
  27. float orientation;
  28. float invInertia;
  29. ParticleData()
  30. {
  31. orientation = 2.0f ;
  32. invInertia = 3.0f;
  33. }
  34. };
  35.  
  36. template <>
  37. struct ParticleData<Space3>
  38. {
  39. float test;
  40. ParticleData()
  41. {
  42. test = 100500.0f;
  43. }
  44. };
  45.  
  46. template<typename T>
  47. struct Dumper
  48. {
  49. };
  50.  
  51. template <typename T>
  52. struct Overload{};
  53.  
  54. template<typename Space,typename AwesomeParam>
  55. struct ParticleSystem
  56. {
  57. struct Particle : public ParticleData<Space>
  58. {
  59. float someGenericStuff;
  60. };
  61.  
  62. void DumpParticles();
  63.  
  64. ParticleSystem()
  65. {
  66. particles.push_back(Particle());
  67. }
  68. std::vector<Particle> particles;
  69. };
  70.  
  71. template<>
  72. struct Dumper<ParticleSystem<Space2,char> >
  73. {
  74. static void Do(ParticleSystem<Space2,char>::Particle& p)
  75. {
  76. printf("2: %f %f\n", p.orientation, p.invInertia );
  77. }
  78. };
  79.  
  80. template<>
  81. struct Dumper<ParticleSystem<Space3,float> >
  82. {
  83. static void Do(ParticleSystem<Space3,float>::Particle& p)
  84. {
  85. printf("3: %f\n", p.test );
  86. }
  87. };
  88.  
  89. template<typename Space,typename AwesomeParam>
  90. void ParticleSystem<Space,AwesomeParam>::DumpParticles()
  91. {
  92. for(size_t particleIndex = 0; particleIndex < particles.size(); particleIndex++)
  93. {
  94. Dumper<ParticleSystem<Space,AwesomeParam> >::Do(particles[particleIndex]);
  95. }
  96. }
  97.  
  98.  
  99. int main()
  100. {
  101. ParticleSystem<Space3,float> p3;
  102. ParticleSystem<Space2,char> p2;
  103. p3.DumpParticles();
  104. p2.DumpParticles();
  105. return 0;
  106. }
  107.  
Success #stdin #stdout 0s 2816KB
stdin
Standard input is empty
stdout
3: 100500.000000
2: 2.000000 3.000000