Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template<typename T>
- struct Foo
- {
- T bar;
- void doSomething(T param) {/* do stuff using T */}
- };
- // somewhere in a .cpp
- Foo<int> f;
- struct FooInt
- {
- int bar;
- void doSomething(int param) {/* do stuff using int */}
- }
- // Foo.h
- template <typename T>
- struct Foo
- {
- void doSomething(T param);
- };
- #include "Foo.tpp"
- // Foo.tpp
- template <typename T>
- void Foo<T>::doSomething(T param)
- {
- //implementation
- }
- // Foo.h
- // no implementation
- template <typename T> struct Foo { ... };
- //----------------------------------------
- // Foo.cpp
- // implementation of Foo's methods
- // explicit instantiations
- template class Foo<int>;
- template class Foo<float>;
- // You will only be able to use Foo with int or float
- template class vector<int>;
- template < typename ... >
- class MyClass
- {
- int myMethod()
- {
- // Not just declaration. Add method implementation here
- }
- };
- #ifndef MyTemplate_h
- #define MyTemplate_h 1
- template <class T>
- class MyTemplate
- {
- public:
- MyTemplate(const T& rt);
- void dump();
- T t;
- };
- #endif
- #include "MyTemplate.h"
- #include <iostream>
- template <class T>
- MyTemplate<T>::MyTemplate(const T& rt)
- : t(rt)
- {
- }
- template <class T>
- void MyTemplate<T>::dump()
- {
- cerr << t << endl;
- }
- #ifndef MyInstantiatedTemplate_h
- #define MyInstantiatedTemplate_h 1
- #include "MyTemplate.h"
- typedef MyTemplate< int > MyInstantiatedTemplate;
- #endif
- #include "MyTemplate.cpp"
- template class MyTemplate< int >;
- #include "MyInstantiatedTemplate.h"
- int main()
- {
- MyInstantiatedTemplate m(100);
- m.dump();
- return 0;
- }
Add Comment
Please, Sign In to add comment