pastebin - collaborative debugging tool
rovema.kpaste.net RSS


lambda_function_member_of_struct_array1.cpp
Posted by Anonymous on Mon 14th Feb 2022 13:38
raw | new post

  1. /*
  2.  * Lambda functions as members of a struct in an arrway demo
  3.  *
  4.  * Compile with:
  5.  * $ g++ -std=gnu++17 -Wall lam1.cpp #
  6.  * $ clang++ -std=gnu++17 -Wall lam1.cpp #
  7.  *
  8.  * Written by Roland Mainz <roland.mainz@nrubsig.org>
  9.  */
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <functional>
  14.  
  15. struct foo
  16. {
  17.         const char *name;
  18.         const std::function<void()> func;
  19. };
  20.  
  21. int main(int ac, char *av[])
  22. {
  23.         struct foo f[4] = {
  24.                 { .name = "one",        .func = [=](void){ puts("insane");      } },
  25.                 { .name = "two",        .func = [=](void){ puts("in");          } },
  26.                 { .name = "three",      .func = [=](void){ puts("the");         } },
  27.                 { .name = "four",       .func = [=](void){ puts("mainframe");   } },
  28.         };
  29.  
  30.         for(int i=0 ; i < 4 ; i++) {
  31.                 f[i].func();
  32.         }
  33.        
  34.         return EXIT_SUCCESS;
  35. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with {%HIGHLIGHT}




All content is user-submitted.
The administrators of this site (kpaste.net) are not responsible for their content.
Abuse reports should be emailed to us at