Source File: ex50a.cpp

#include <fmt/core.h>
#include <vector>
#include <ranges>
#include <algorithm>

auto add_func(auto a, auto b) {
  return a + b;
}

int main() {
  using namespace std::ranges;

  std::vector<int> the_ints{ 0, 100, 34, 82, 126};
  std::vector<float> the_floats{ 0.1f, 100.1f, 34.1f, 82.1f, 126.1f };

  // do it manually to see add_func in action
  {
    int int_res = 0;

    for(int i : the_ints) {
      int_res += i;
    }

    fmt::println("manual int result: {}", int_res);

    float float_res = 0;

    for(float i : the_floats) {
      float_res += i;
    }

    fmt::println("manual float result: {}", float_res);
  }

  // now use a lambda, which is way easier
  {
    auto add = [](auto a, auto b) -> auto { return a + b; };

    auto int_res = fold_left(the_ints, 0, add);
    auto float_res = fold_left(the_floats, 0.0f, add);

    fmt::println("ints={}, floats={}", int_res, float_res);
  }

  // now with a add_func, without add_func<type,type> you get an error
  {
    auto int_res = fold_left(the_ints, 0, add_func<int,int>);
    auto float_res = fold_left(the_floats, 0.0f, add_func<float,float>);

    fmt::println("ints={}, floats={}", int_res, float_res);
  }
}