fork download
  1. #include <iostream>
  2.  
  3. ////////////////////////////////////////////////////////////////////////////////
  4. // ASSUME EVERYTHING IN THIS SECTION IS PART OF AN API AND IS NOT MY OWN CODE...
  5. // I DO NOT HAVE THE SOURCE AND IT CANNOT BE MODIFIED
  6.  
  7. typedef void (*CALLBACK)(std::string message);
  8.  
  9. void call_callback(CALLBACK cb, std::string message) {
  10. cb(message);
  11. }
  12.  
  13. ////////////////////////////////////////////////////////////////////////////////
  14.  
  15. // Our callback takes a string *and* an integer argument
  16. void callback_function(std::string message, int data)
  17. {
  18. std::cout << message << ' ' << data << '\n';
  19. }
  20.  
  21. int main()
  22. {
  23. // Using lambdas...
  24. call_callback([/*empty!*/](std::string message)
  25. {
  26. // Call the function as defined in the previous snippet
  27. callback_function(message, 42); // Pass the value directly
  28. }, "Foobar");
  29. }
  30.  
Success #stdin #stdout 0s 4500KB
stdin
Standard input is empty
stdout
Foobar 42