Created
May 13, 2015 09:40
-
-
Save danhper/8f5d44eb8cfee370c431 to your computer and use it in GitHub Desktop.
Template specialization
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
class foo | |
{ | |
public: | |
foo() {} | |
}; | |
class bar | |
{ | |
public: | |
bar() {} | |
}; | |
template<typename T> | |
T get() | |
{ | |
throw "does not exist"; | |
} | |
template <> foo get <foo>() | |
{ | |
return foo(); | |
} | |
template <> bar get <bar>() | |
{ | |
return bar(); | |
} |
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
#include <iostream> | |
#include <typeinfo> | |
#include "foo.h" | |
int main(void) | |
{ | |
foo f = get<foo>(); | |
bar b = get<bar>(); | |
std::cout << typeid(f).name() << std::endl; | |
std::cout << typeid(b).name() << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment