Created
July 26, 2022 23:20
-
-
Save atomicdog/852f4ad88226720fabe6a3d105d5aa66 to your computer and use it in GitHub Desktop.
C++ lambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace foo { | |
uint8_t bar(uint8_t xyz) {return xyz +1;} | |
uint8_t baz(uint8_t xyz) {return xyz +2;} | |
} | |
namespace dog { | |
uint8_t fido(uint8_t xyz) {return xyz +3;} | |
uint8_t sparky(uint8_t xyz){return xyz +4;} | |
} | |
typedef uint8_t (*FnPtr) (uint8_t c); | |
uint8_t (*table[])(uint8_t c)= {foo::bar,foo::baz,dog::fido,dog::sparky}; | |
int main() | |
{ | |
volatile uint8_t arg; | |
uint8_t cmd = 0; | |
do | |
{ | |
auto lambda_fn = [&table](uint8_t c) -> FnPtr { return table[c]; }; | |
(lambda_fn(cmd))(arg); | |
if (3 > cmd) | |
++cmd; | |
else | |
cmd = 0; | |
}while (1); | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment