Guest User

Untitled

a guest
Aug 26th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. template<typename T>
  2. struct Foo
  3. {
  4. T bar;
  5. void doSomething(T param) {/* do stuff using T */}
  6. };
  7.  
  8. // somewhere in a .cpp
  9. Foo<int> f;
  10.  
  11. struct FooInt
  12. {
  13. int bar;
  14. void doSomething(int param) {/* do stuff using int */}
  15. }
  16.  
  17. // Foo.h
  18. template <typename T>
  19. struct Foo
  20. {
  21. void doSomething(T param);
  22. };
  23.  
  24. #include "Foo.tpp"
  25.  
  26. // Foo.tpp
  27. template <typename T>
  28. void Foo<T>::doSomething(T param)
  29. {
  30. //implementation
  31. }
  32.  
  33. // Foo.h
  34.  
  35. // no implementation
  36. template <typename T> struct Foo { ... };
  37.  
  38. //----------------------------------------
  39. // Foo.cpp
  40.  
  41. // implementation of Foo's methods
  42.  
  43. // explicit instantiations
  44. template class Foo<int>;
  45. template class Foo<float>;
  46. // You will only be able to use Foo with int or float
  47.  
  48. template class vector<int>;
  49.  
  50. template < typename ... >
  51. class MyClass
  52. {
  53.  
  54. int myMethod()
  55. {
  56. // Not just declaration. Add method implementation here
  57. }
  58. };
  59.  
  60. #ifndef MyTemplate_h
  61. #define MyTemplate_h 1
  62.  
  63. template <class T>
  64. class MyTemplate
  65. {
  66. public:
  67. MyTemplate(const T& rt);
  68. void dump();
  69. T t;
  70. };
  71.  
  72. #endif
  73.  
  74. #include "MyTemplate.h"
  75. #include <iostream>
  76.  
  77. template <class T>
  78. MyTemplate<T>::MyTemplate(const T& rt)
  79. : t(rt)
  80. {
  81. }
  82.  
  83. template <class T>
  84. void MyTemplate<T>::dump()
  85. {
  86. cerr << t << endl;
  87. }
  88.  
  89. #ifndef MyInstantiatedTemplate_h
  90. #define MyInstantiatedTemplate_h 1
  91. #include "MyTemplate.h"
  92.  
  93. typedef MyTemplate< int > MyInstantiatedTemplate;
  94.  
  95. #endif
  96.  
  97. #include "MyTemplate.cpp"
  98.  
  99. template class MyTemplate< int >;
  100.  
  101. #include "MyInstantiatedTemplate.h"
  102.  
  103. int main()
  104. {
  105. MyInstantiatedTemplate m(100);
  106. m.dump();
  107. return 0;
  108. }
Add Comment
Please, Sign In to add comment