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