#include <iostream>
#include <functional>
#include <cmath>

template <typename Result, typename Arg>
class Function
{
public:
        Function(const std::function<Result(Arg)> & f) : f_(f) {}

        Result operator() (Arg arg) const { return f_(arg); }

        Function compose(const Function& g) const
        {
                return { [this, g](Arg arg) { return f_(g(arg)); } };
        }

private:
        std::function<Result(Arg)> f_;
};

int main()
{
        auto f = Function<double, double>([](double x) { return exp(x); });
        auto g = Function<double, double>([](double x) { return pow(x, 2); });
        auto h = Function<double, double>([](double x) { return -x + 1; });
        auto c = f.compose(g).compose(h);
        std::cout << c(3.0) << std::endl;

        return 0;
}