fork download
  1. #include <functional>
  2. #include <iostream>
  3. /** Global test function callback.
  4.  *
  5.  * Holds a callback function that can be invoked with a uint32_t value.
  6.  * Used for testing purposes in the example to demonstrate function pointers and lambdas.
  7.  */
  8. std::function<void(uint32_t)> onTestFunction = nullptr;
  9.  
  10. /** Example function.
  11.  *
  12.  * Demonstrates calling a callback function if it is set.
  13.  * This function checks if the global callback is assigned and invokes it with the provided value.
  14.  * Useful for illustrating callback mechanisms in C++.
  15.  *
  16.  * @param value The uint32_t value to pass to the callback.
  17.  */
  18. void example(uint32_t value) {
  19. std::cout << "Value: " << value << std::endl;
  20. }
  21.  
  22. /** Main entry point.
  23.  *
  24.  * Demonstrates the usage of the example function with a callback.
  25.  * Sets up a lambda as the callback that calls example recursively, then calls example with 42.
  26.  * This showcases C++11 features like lambdas and std::function.
  27.  *
  28.  * @return Always returns 0 indicating successful execution.
  29.  */
  30. int main() {
  31. onTestFunction = [](uint32_t v) { example(v); };
  32. example(42);
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Value: 42