Skip to content

Instantly share code, notes, and snippets.

@kakkun61
Created June 26, 2012 05:31
Show Gist options
  • Save kakkun61/2993526 to your computer and use it in GitHub Desktop.
Save kakkun61/2993526 to your computer and use it in GitHub Desktop.
[solved] Why is std::tr1::bad_function_call thrown?
#include<iostream>
#include<functional>
#include<cmath>
using namespace std;
using namespace std::tr1;
template<typename Param, typename Middle, typename Result>
function<Result(Param)> compose(const function<Result(Middle)>& f, const function<Middle(Param)>& g) {
return [f, g](Param x){return f(g(x));};
}
int main(void) {
function<double(double)> f = [](double x){return exp(x);};
function<double(double)> g = [](double x){return pow(x, 2);};
function<double(double)> h = [](double x){return -x + 1;};
function<double(double)> &composed = compose(compose(f, g), h);
cout << composed(10) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment