Skip to content

Instantly share code, notes, and snippets.

@milesrout
Last active August 29, 2015 14:21
Show Gist options
  • Save milesrout/2701d4af794542f186e9 to your computer and use it in GitHub Desktop.
Save milesrout/2701d4af794542f186e9 to your computer and use it in GitHub Desktop.
struct Super {
virtual Super* id() {
return this;
}
};
struct X : Super {};
struct Y : Super {};
template <typename A>
A* id(A* a) {
return a;
}
// if you have an X* called x and a Y* called y, then:
// id(x) is shorthand for id<X>(x), and id(y) is shorthand for id<Y>(y), this is type inference at play.
// id(x) is of type X*. id(y) is of type Y*. This is easy to see, you just substitute in X or Y into the template
// That's why it's called a template.
// if you call x->id() then you get a Super. if you call y->id() then you get a Super.
// this is because you are calling the function Super::id.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment