callbacks.cpp 846 B

1234567891011121314151617181920212223242526272829303132333435
  1. // callbacks
  2. // not meant to be read top to bottom, but rather following the function calls, starting from main
  3. #include <functional> // to demo std::function style callbacks
  4. class handle_t
  5. {
  6. public:
  7. ~handle_t(); // cancels the request
  8. };
  9. class result_t; // what we want
  10. class error_t; // what may happen instead
  11. handle_t request_c(void(*)(void*, result_t), void(*)(void*, error_t), void*) { return {}; }
  12. handle_t request_f(std::function<void(result_t)>, std::function<void(error_t)>) { return {}; }
  13. template <typename Callback>
  14. // insert sfinae or require for callback, accepting either result or error
  15. handle_t request_m(Callback) { return {}; }
  16. void on_completion(result_t);
  17. void on_completion(error_t);
  18. int main(int argc, char const* argv[])
  19. {
  20. auto handle = request_m([](auto result)
  21. {
  22. on_completion(result);
  23. });
  24. return 0;
  25. }