fork download
  1. #include <iostream>
  2. #include <functional>
  3.  
  4. struct protocol_callbacks
  5. {
  6. using func_t = std::function<int(uint8_t*, size_t)>;
  7.  
  8. protocol_callbacks(func_t a_reader, func_t a_writer) :
  9. reader(a_reader),
  10. writer(a_writer) {}
  11. func_t reader;
  12. func_t writer;
  13. };
  14.  
  15. int writer(uint8_t*, size_t)
  16. {
  17. std::cout << "function writer\n";
  18. return 0;
  19. }
  20. int reader(uint8_t*, size_t)
  21. {
  22. std::cout << "function writer\n";
  23. return 0;
  24. }
  25.  
  26. int bad_writer(uint16_t*, size_t) { return 0; }
  27.  
  28. int main ()
  29. {
  30. int x = 9;
  31. protocol_callbacks pc1(reader, writer);
  32. protocol_callbacks pc2([=](uint8_t*, size_t)
  33. {
  34. std::cout << "lambda reader: " << x << "\n";
  35. return 0;
  36. },
  37. [=](uint8_t*, size_t)
  38. {
  39. std::cout << "lambda writer: " << x << "\n";
  40. return 0;
  41. });
  42.  
  43. pc1.reader(nullptr, 0);
  44. pc1.writer(nullptr, 0);
  45.  
  46. pc2.reader(nullptr, 0);
  47. pc2.writer(nullptr, 0);
  48. //protocol_callbacks pc3(bad_writer, reader);
  49. }
  50.  
Success #stdin #stdout 0s 3032KB
stdin
Standard input is empty
stdout
function writer
function writer
lambda reader: 9
lambda writer: 9