Skip to content

Instantly share code, notes, and snippets.

@Garciat
Created October 1, 2016 09:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garciat/677e95fc3fe813e1646e23fff00940d3 to your computer and use it in GitHub Desktop.
Save Garciat/677e95fc3fe813e1646e23fff00940d3 to your computer and use it in GitHub Desktop.
#include <utility>
template<typename T, T... PMFs>
struct method_choice;
template<typename C, typename R, R C::* PMF, R C::*... PMFs>
struct method_choice<R C::*, PMF, PMFs...> {
template<typename... A>
method_choice(int choice, C &subject, A... args) {
if (choice == 0) {
(subject.*PMF)(std::forward<A>(args)...);
} else {
method_choice<R C::*, PMFs...>(choice - 1, subject, std::forward<A>(args)...);
}
}
};
template<typename C, typename R>
struct method_choice<R C::*> {
template<typename... A>
method_choice(int, C &, A...) {
// die?
}
};
extern volatile int poop;
struct what {
void f(int x) { poop += x; }
void g(int x) { poop *= x; }
void h(int x) { poop -= x; }
};
void hello(int k) {
what w;
method_choice<void (what::*)(int), &what::f, &what::g, &what::h>(k, w, 42);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment