- /*
- * Lambda functions as members of a struct in an arrway demo
- *
- * Compile with:
- * $ g++ -std=gnu++17 -Wall lam1.cpp #
- * $ clang++ -std=gnu++17 -Wall lam1.cpp #
- *
- * Written by Roland Mainz <roland.mainz@nrubsig.org>
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <functional>
- struct foo
- {
- const char *name;
- const std::function<void()> func;
- };
- int main(int ac, char *av[])
- {
- struct foo f[4] = {
- { .name = "one", .func = [=](void){ puts("insane"); } },
- { .name = "two", .func = [=](void){ puts("in"); } },
- { .name = "three", .func = [=](void){ puts("the"); } },
- { .name = "four", .func = [=](void){ puts("mainframe"); } },
- };
- for(int i=0 ; i < 4 ; i++) {
- f[i].func();
- }
- return EXIT_SUCCESS;
- }
lambda_function_member_of_struct_array1.cpp
Posted by Anonymous on Mon 14th Feb 2022 13:38
raw | new post
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.