Skip to content

Instantly share code, notes, and snippets.

@glampert
Created January 22, 2017 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glampert/127eb65a71020206d970f0ec64394666 to your computer and use it in GitHub Desktop.
Save glampert/127eb65a71020206d970f0ec64394666 to your computer and use it in GitHub Desktop.
//
// 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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment