#include <iostream>


template<typename T, typename LogFunc>
class Foo {
  public:
    Foo(const T& t, LogFunc fn) : t_(t), lfn_(fn) {}

    template<typename LFN>
    Foo& operator+=(const Foo<T, LFN>& other) {
      lfn_(t_, other());
      t_ += other();
      return *this;
    }
    T operator()() const { return t_; }

  private:
    T t_;
    LogFunc lfn_;
};

template<typename T, typename LogFunc>
std::ostream& operator<<(std::ostream& o, const Foo<T, LogFunc>& f) {
  return o << f();
}

// It's actually amazing that this works, and I presume that it works because it
// is only used in an inline function, from which it can be resolved at
// compile-time.
struct Noop {
  template<typename...A>
  void operator()(A...) { };
};

template<typename T, typename LogFunc=Noop>
Foo<T, LogFunc> make_foo(const T& t, LogFunc func=LogFunc()) {
  return Foo<T, LogFunc>(t, func);
}

template<typename T>
void log(std::ostream& o, const T& a, const T& b) {
  o << "a: " << a << " b: " << b << std::endl;
}

int main(int argc, char**argv) {
  auto f1 = make_foo(3.141592653);
  auto f2 = make_foo(2.5, [&](double a, double b) { log(std::cout, a, b); });
  f1 += f2;
  f2 += f1;
  std::cout << "f1: " << f1 << " f2: " << f2 << std::endl;
  return 0;
}