fork download
  1. #include <algorithm>
  2. #include <type_traits>
  3.  
  4. namespace fixed {
  5. namespace std {
  6. template <typename InputT, typename T>
  7. auto accumulate (InputT from, InputT to, T init) ->
  8. typename ::std::enable_if <::std::is_same<decltype(init+*from),decltype(init+*from+*from)>::value,
  9. decltype(init+*from)>::type
  10. {
  11. return ::std::accumulate(from, to, decltype(init+*from)(init));
  12. }
  13.  
  14. template <typename InputT, typename T, typename Op>
  15. auto accumulate (InputT from, InputT to, T init, Op op) ->
  16. typename ::std::enable_if <::std::is_same<decltype(op(init,*from)),decltype(op(op(init,*from),*from))>::value,
  17. decltype(op(init,*from))>::type
  18. {
  19. return ::std::accumulate(from, to, decltype(op(init,*from))(init));
  20. }
  21.  
  22. }
  23. }
  24.  
  25. #include <iterator>
  26. #include <iostream>
  27. int main ()
  28. {
  29. int foo[] {1,2,3};
  30. std::cout << fixed::std::accumulate (std::begin(foo), std::end(foo), 0.5) << std::endl;
  31. double bar[] {1.1,2.2,3.3};
  32. std::cout << fixed::std::accumulate (std::begin(bar), std::end(bar), 0) << std::endl;
  33. }
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
6.5
6.6