Last active
August 29, 2015 14:16
-
-
Save mattypiper/0e49e262497865d5494e to your computer and use it in GitHub Desktop.
C++ Non-Type Function Templates
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 <string> | |
using namespace std; | |
struct Object | |
{ | |
Object(string name) : name(name) {} | |
void func() { cout << "Object func " << name << endl; } | |
string name; | |
}; | |
Object julian("Julian Hamilton"); | |
Object kim("Kim Moyes"); | |
template<Object* obj> | |
struct X | |
{ | |
static void wrap() | |
{ | |
obj->func(); | |
} | |
}; | |
template<Object* obj> | |
static void funcWrap() | |
{ | |
obj->func(); | |
} | |
typedef void (*pFunc)(); | |
int main(int argc, char** argv) | |
{ | |
pFunc f1 = X<&billy>::wrap; | |
pFunc f2 = funcWrap<&brandon>; | |
f1(); | |
f2(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment