Last active
May 24, 2017 06:55
-
-
Save jaguilar/5479988 to your computer and use it in GitHub Desktop.
So you think you know C++ #1
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
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.