Created
January 22, 2017 16:20
-
-
Save glampert/127eb65a71020206d970f0ec64394666 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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