Skip to content

Instantly share code, notes, and snippets.

@danhper
Created May 13, 2015 09:40
Show Gist options
  • Save danhper/8f5d44eb8cfee370c431 to your computer and use it in GitHub Desktop.
Save danhper/8f5d44eb8cfee370c431 to your computer and use it in GitHub Desktop.
Template specialization
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();
}
#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