Skip to content

Instantly share code, notes, and snippets.

@Kontinuation
Created September 27, 2013 17:51
Show Gist options
  • Save Kontinuation/6732367 to your computer and use it in GitHub Desktop.
Save Kontinuation/6732367 to your computer and use it in GitHub Desktop.
Extends the number of arguments, can be regarded as an inverse transformation of currying
#include <iostream>
#include <functional>
using namespace std;
template <typename ... _> struct extend;
template <typename t_ret, typename ... t_args, typename ... t_arge>
struct extend <t_ret(t_args ...) , t_arge ...> {
static std::function<t_ret(t_args ..., t_arge ...)> impl (std::function<t_ret(t_args ...)> f) {
return [=] (t_args ... args, t_arge ... arge) {
return f(args ...);
};
}
};
int main(int argc, char *argv[])
{
std::function<void(float, int, double)> f;
int z;
auto a = [&](float x, int, double){z = x; cout << z;};
auto b = [&](float x, int){z = x; cout << z;};
auto c = [&](double x){z = x; cout << z;};
f = a;
f(1, 2, 3);
//f = b; //?!?!
f = extend<void(float, int), double>::impl(b);
f(4, 5, 6);
//f = c; //?!?!
f = extend<void(float), int, double>::impl(c);
f(7, 8, 9);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment