Skip to content

Instantly share code, notes, and snippets.

@isaac-weisberg
Created November 11, 2018 04:27
Show Gist options
  • Select an option

  • Save isaac-weisberg/cf2be7e3cafa24932f11109c1871a341 to your computer and use it in GitHub Desktop.

Select an option

Save isaac-weisberg/cf2be7e3cafa24932f11109c1871a341 to your computer and use it in GitHub Desktop.
#include <tuple>
auto foo(int initialBaz) {
int baz = initialBaz;
auto getBaz = [&] {
return baz;
};
auto setBaz = [&](int newValue) {
baz = newValue;
};
// Keep in mind, this one is copied compared to new std::tuple,
// which requires explicit template arguments
return std::make_tuple(
getBaz,
setBaz
);
}
void usage() {
auto fooObject = foo(3);
auto currValue = std::get<0>(fooObject)(); // 3
std::get<1>(fooObject)(5);
auto newValue = std::get<0>(fooObject)(); // 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment