#include <iostream>
template <typename F0, typename... F>
class Composer2 {
F0 f0_;
Composer2<F...> tail_;
public:
Composer2(F0 f0, F... f) : f0_(f0), tail_(f...) {}
template <typename T>
T operator() (const T& x) const {
return f0_(tail_(x));
}
};
template <typename F>
class Composer2<F> {
F f_;
public:
Composer2(F f) : f_(f) {}
template <typename T>
T operator() (const T& x) const {
return f_(x);
}
};
template <typename... F>
Composer2<F...> compose2(F... f) {
return Composer2<F...>(f...);
}
int f(int x) { return x + 1; }
int g(int x) { return x * 2; }
int h(int x) { return x - 1; }
int main() {
std::cout << compose2(f, g, h)(42);
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKdGVtcGxhdGUgPHR5cGVuYW1lIEYwLCB0eXBlbmFtZS4uLiBGPgpjbGFzcyBDb21wb3NlcjIgewoJRjAgZjBfOwoJQ29tcG9zZXIyPEYuLi4+IHRhaWxfOwpwdWJsaWM6CglDb21wb3NlcjIoRjAgZjAsIEYuLi4gZikgOiBmMF8oZjApLCB0YWlsXyhmLi4uKSB7fQoKCXRlbXBsYXRlIDx0eXBlbmFtZSBUPgoJVCBvcGVyYXRvcigpIChjb25zdCBUJiB4KSBjb25zdCB7CgkJcmV0dXJuIGYwXyh0YWlsXyh4KSk7Cgl9Cn07Cgp0ZW1wbGF0ZSA8dHlwZW5hbWUgRj4KY2xhc3MgQ29tcG9zZXIyPEY+IHsKCUYgZl87CnB1YmxpYzoKICAgIENvbXBvc2VyMihGIGYpIDogZl8oZikge30KCgl0ZW1wbGF0ZSA8dHlwZW5hbWUgVD4KCVQgb3BlcmF0b3IoKSAoY29uc3QgVCYgeCkgY29uc3QgewoJCXJldHVybiBmXyh4KTsKCX0KfTsKCnRlbXBsYXRlIDx0eXBlbmFtZS4uLiBGPgpDb21wb3NlcjI8Ri4uLj4gY29tcG9zZTIoRi4uLiBmKSB7CglyZXR1cm4gQ29tcG9zZXIyPEYuLi4+KGYuLi4pOwp9CgppbnQgZihpbnQgeCkgeyByZXR1cm4geCArIDE7IH0KaW50IGcoaW50IHgpIHsgcmV0dXJuIHggKiAyOyB9CmludCBoKGludCB4KSB7IHJldHVybiB4IC0gMTsgfQoKaW50IG1haW4oKSB7CiAgc3RkOjpjb3V0IDw8IGNvbXBvc2UyKGYsIGcsIGgpKDQyKTsKICByZXR1cm4gMDsKfQ==