Skip to content

Instantly share code, notes, and snippets.

@mattypiper
Last active August 29, 2015 14:16
Show Gist options
  • Save mattypiper/0e49e262497865d5494e to your computer and use it in GitHub Desktop.
Save mattypiper/0e49e262497865d5494e to your computer and use it in GitHub Desktop.
C++ Non-Type Function Templates
#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