Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Overriding a member method of a template class/struct by defining a specialized implementation.
- //
- #include <cstdio>
- #include <typeinfo>
- template<typename T>
- struct Foo
- {
- void bar()
- {
- std::printf("Base Foo<%s>::bar()\n", typeid(T).name());
- }
- };
- template<>
- void Foo<int>::bar()
- {
- std::printf("Specialized Foo<int>::bar()\n");
- }
- template<>
- void Foo<float>::bar()
- {
- std::printf("Specialized Foo<float>::bar()\n");
- }
- int main()
- {
- Foo<char> cFoo;
- Foo<int> iFoo;
- Foo<float> fFoo;
- cFoo.bar(); // Base implementation
- iFoo.bar(); // Specialized
- fFoo.bar(); // Specialized
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement