Skip to content

Instantly share code, notes, and snippets.

@atomicdog
Created July 26, 2022 23:20
Show Gist options
  • Save atomicdog/852f4ad88226720fabe6a3d105d5aa66 to your computer and use it in GitHub Desktop.
Save atomicdog/852f4ad88226720fabe6a3d105d5aa66 to your computer and use it in GitHub Desktop.
C++ lambda
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