Last active
August 29, 2015 14:21
-
-
Save milesrout/2701d4af794542f186e9 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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