fork download
  1. // Showing how the use of template template parameters is probably
  2. // the most robust solution.
  3. // DD
  4. //
  5. #include <iostream>
  6. #include <complex>
  7.  
  8. template <typename T>
  9. struct GBM
  10. {
  11. T operator () () const
  12. {
  13. return T(-1);
  14. }
  15. };
  16. template <typename T>
  17. struct Jump
  18. {
  19. T operator () () const
  20. {
  21. return T(99999);
  22. }
  23. };
  24.  
  25. template <typename T, template <typename T> class SDE>
  26. struct FDM
  27. {
  28. T operator ()() const
  29. {
  30. SDE<T> sde;
  31. return T(0.01)*sde();
  32. }
  33. };
  34.  
  35. template <typename T, template <typename T> class SDE, template<typename T, template <typename T> class SDE> class FDM>
  36. struct FDM_II
  37. {
  38. FDM_II() {}
  39. T operator ()() const
  40. {
  41. SDE<T> sde;
  42. FDM<T, SDE> fdm;
  43.  
  44. return fdm()*sde();
  45. }
  46. };
  47.  
  48. template <typename T, typename SDE, typename FDM>
  49. struct FDM_NOT_II
  50. {
  51. FDM_NOT_II() {}
  52. T operator ()() const
  53. {
  54. SDE sde;
  55. FDM fdm;
  56.  
  57. return fdm()*sde();
  58. }
  59. };
  60. int main()
  61. {
  62. FDM<double, GBM> mySolver;
  63. std::cout << mySolver() << std::endl;
  64.  
  65. FDM<double, Jump> mySolver2;
  66. std::cout << mySolver2() << std::endl;
  67.  
  68. // The best solution IMO
  69. FDM_II<float, GBM, FDM> fdmII;
  70. FDM_II<std::complex<int>, Jump, FDM> fdmIIA;
  71. std::cout << "last one: " << fdmIIA() << std::endl;
  72.  
  73. // Old style way and incorrect, but compiles.
  74. FDM_NOT_II<std::complex<int>, Jump<double>, FDM<int, GBM>> fdmIIB;
  75.  
  76. // Correct way the old way
  77. FDM_NOT_II<std::complex<int>, Jump<std::complex<int>>, FDM<std::complex<int>, GBM>> fdmIIC;
  78.  
  79. return 0;
  80. }
  81.  
  82.  
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
-0.01
999.99
last one: (0,0)