https://gcc.gnu.org/bugzilla/show_bug.cgi?id=108667

--- Comment #4 from Alvaro Begue <alvaro.begue at gmail dot com> ---
Original code:

#include <functional>
#include <vector>
#include <iostream>

template <typename ReturnType, typename... ArgumentTypes>
class Signal {
public:
  using Slot = std::function<ReturnType(ArgumentTypes...)>;
  using FoldingFunction = std::function<ReturnType(ReturnType, ReturnType)>;

  Signal(FoldingFunction fold, ReturnType initial)
    : fold(fold), initial(initial) {}

  void connect(Slot slot) {
    slots.push_back(slot);
  }

  ReturnType operator() (ArgumentTypes... arguments) {
    ReturnType result = initial;

    for (const auto &slot : slots)
      result = fold(result, slot(arguments...));

    return result;
  }

private:
  std::vector<Slot> slots;
  FoldingFunction fold;
  ReturnType initial;
};


int four() { return 4; }

int five() { return 5; }

int main() {
  Signal<int> get_total([](int cumulative_value, int new_term){
    return cumulative_value + new_term;
  }, 0);
  get_total.connect(four);
  get_total.connect(five);
  std::cout << get_total() << '\n';
}

Reply via email to