Skip to content

Instantly share code, notes, and snippets.

@fortheday
Last active January 30, 2018 08:35
Show Gist options
  • Save fortheday/fceb470f7740e2698769b53403d74d51 to your computer and use it in GitHub Desktop.
Save fortheday/fceb470f7740e2698769b53403d74d51 to your computer and use it in GitHub Desktop.
int a = 1, b = 2;
[&] // capture local variables as &a, &b
{
a = 3; // &a = 3
b = 4;
}(); // instant execution
[=] // capture local variables const a, const b
{
a = 1; // COMPILE ERROR
const_cast<int &>(b) = 1; // changes copied another b
}();
std::cout << a << ' ' << b << std::endl;// 3 4
int q = 1;
[q]() mutable { q = 2; }(); // changes copied another q
std::cout << q << std::endl; // 1
[&x = q]() { x = 5; }(); // update original q through reference x
std::cout << q << std::endl; // 5
auto cfn = []() constexpr -> int { return 'A'; };
std::cout << cfn() << std::endl; // 65
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment