fork download
  1. #include <iostream>
  2. #include <functional>
  3. #include <cmath>
  4.  
  5. template <typename Result, typename Arg>
  6. class Function
  7. {
  8. public:
  9. Function(const std::function<Result(Arg)> & f) : f_(f) {}
  10.  
  11. Result operator() (Arg arg) const { return f_(arg); }
  12.  
  13. Function compose(const Function& g) const
  14. {
  15. return { [this, g](Arg arg) { return f_(g(arg)); } };
  16. }
  17.  
  18. private:
  19. std::function<Result(Arg)> f_;
  20. };
  21.  
  22. int main()
  23. {
  24. auto f = Function<double, double>([](double x) { return exp(x); });
  25. auto g = Function<double, double>([](double x) { return pow(x, 2); });
  26. auto h = Function<double, double>([](double x) { return -x + 1; });
  27. auto c = f.compose(g).compose(h);
  28. std::cout << c(3.0) << std::endl;
  29.  
  30. return 0;
  31. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In lambda function:
prog.cpp:15:54: error: invalid type argument
In file included from prog.cpp:2:0:
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/functional: In static member function 'static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = double, _Functor = Function<Result, Arg>::compose(const Function<Result, Arg>&) const [with Result = double, Arg = double, Function<Result, Arg> = Function<double, double>]::<lambda(double)>, _ArgTypes = {double}]':
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/functional:2103:6:   instantiated from 'std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = Function<Result, Arg>::compose(const Function<Result, Arg>&) const [with Result = double, Arg = double, Function<Result, Arg> = Function<double, double>]::<lambda(double)>, _Res = double, _ArgTypes = {double}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<double(double)>::_Useless]'
prog.cpp:15:68:   instantiated from 'Function<Result, Arg> Function<Result, Arg>::compose(const Function<Result, Arg>&) const [with Result = double, Arg = double, Function<Result, Arg> = Function<double, double>]'
prog.cpp:27:29:   instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/functional:1699:47: error: void value not ignored as it ought to be
prog.cpp: In lambda function:
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/functional:1699:47:   instantiated from 'static _Res std::_Function_handler<_Res(_ArgTypes ...), _Functor>::_M_invoke(const std::_Any_data&, _ArgTypes ...) [with _Res = double, _Functor = Function<Result, Arg>::compose(const Function<Result, Arg>&) const [with Result = double, Arg = double, Function<Result, Arg> = Function<double, double>]::<lambda(double)>, _ArgTypes = {double}]'
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/functional:2103:6:   instantiated from 'std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = Function<Result, Arg>::compose(const Function<Result, Arg>&) const [with Result = double, Arg = double, Function<Result, Arg> = Function<double, double>]::<lambda(double)>, _Res = double, _ArgTypes = {double}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<double(double)>::_Useless]'
prog.cpp:15:68:   instantiated from 'Function<Result, Arg> Function<Result, Arg>::compose(const Function<Result, Arg>&) const [with Result = double, Arg = double, Function<Result, Arg> = Function<double, double>]'
prog.cpp:27:29:   instantiated from here
prog.cpp:15:66: error: return-statement with a value, in function returning 'void'
stdout
Standard output is empty