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. #include <memory>
  8. #include <vector>
  9.  
  10. template <typename T>
  11. struct GBM
  12. {
  13. T operator () () const
  14. {
  15. return T(-1);
  16. }
  17. };
  18. template <typename T>
  19. struct Jump
  20. {
  21. T operator () () const
  22. {
  23. return T(99999);
  24. }
  25. };
  26.  
  27. template <typename T, template <typename T> class SDE>
  28. struct FDM
  29. {
  30. T operator ()() const
  31. {
  32. SDE<T> sde;
  33. return T(0.01)*sde();
  34. }
  35. };
  36.  
  37. template <typename T, template <typename T> class SDE, template<typename T, template <typename T> class SDE> class FDM>
  38. struct FDM_II
  39. {
  40. FDM_II() {}
  41. T operator ()() const
  42. {
  43. SDE<T> sde;
  44. FDM<T, SDE> fdm;
  45.  
  46. return fdm()*sde();
  47. }
  48. };
  49.  
  50. int main()
  51. {
  52. FDM<double, GBM> mySolver;
  53. std::cout << mySolver() << std::endl;
  54.  
  55. FDM<double, Jump> mySolver2;
  56. std::cout << mySolver2() << std::endl;
  57.  
  58. FDM<double, std::allocator> mySolver3;
  59. std::cout << mySolver() << std::endl;
  60.  
  61. FDM_II<float, GBM, FDM> fdmII;
  62. std::cout << "fdmII: " << fdmII() << std::endl;
  63.  
  64. FDM_II<std::complex<int>, Jump, FDM> fdmIIA;
  65. std::cout << "fdmIIA: " << fdmIIA() << std::endl;
  66.  
  67. FDM_II<std::vector<double>, std::allocator, FDM> fdmIIB;
  68. //std::cout << "fdmIIB: " << fdmIIB() << std::endl;
  69.  
  70. return 0;
  71. }
  72.  
  73.  
Success #stdin #stdout 0s 3020KB
stdin
Standard input is empty
stdout
-0.01
999.99
-0.01
fdmII: 0.01
fdmIIA: (0,0)