Skip to content

Instantly share code, notes, and snippets.

@jaguilar
Last active May 24, 2017 06:55
Show Gist options
  • Save jaguilar/5479988 to your computer and use it in GitHub Desktop.
Save jaguilar/5479988 to your computer and use it in GitHub Desktop.
So you think you know C++ #1
// Today we learn about the insane things that can be done with variadic templates.
template <int N> struct A {
template <typename O, typename R, typename... FArgs, typename... TArgs, typename... Args>
R operator()(O* o, R (O::*f)(FArgs...), const std::tuple<TArgs...>& t, Args... args) {
return A<N-1>()(o, f, t, std::get<N>(t), args...);
}
};
template <> struct A<-1> {
template <typename O, typename R, typename... FArgs, typename... TArgs, typename... Args>
R operator()(O* o, R (O::*f)(FArgs...), const std::tuple<TArgs...>& t, Args... args) {
return (o->*f)(args...);
}
};
// What is the effect of function |b|?
template <typename O, typename R, typename... FArgs, typename... TArgs, typename... Args>
R b(O* o, R (O::*f)(FArgs...), const std::tuple<TArgs...>& t) {
return A<sizeof...(TArgs)-1>()(o, f, t);
}
@tvanslyke
Copy link

tvanslyke commented May 24, 2017

Function b() calls the member function pointed to by f on the instance of type O pointed to by o with arguments consisting of elements of the tuple t. The relative order of the arguments passed into the member function is the same is their original order in the tuple.

Edit: Although in reality the effect is probably a compile error when you try to write code that uses b(), then a segfault when you finally run the code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment