Using higher-order lambda function to create object operations

Question | Jan 9, 2017 | nextptr 

A lambda expression that returns another lambda expression or takes a lambda expression as its argument is called a higher-order lambda function. We are going to use a higher-order lambda function to create the common operations of a bank account - credit, debit and getBalance.

enter image description here

Here is a simple Account class with a data member balance_ with no common operations defined.

struct Account {
    Account(int balance):balance_(balance){}
    int balance_;
    /* .. possibly more data members ... */

Following code creates an instance of Account that is reference-captured by a higher-order lambda createAccountOps. Lambda createAccountOps is an operations creator that returns a tuple of common operations on account:

Account account(100);

auto createAccountOps = [&]() {

  return std::make_tuple(
     [&](int amount) {
       account.balance_ += amount;
     [&](int amount){
       account.balance_ -= amount;
       return account.balance_;


Below we call createAccountOps, unpack the returned tuple of operations to 3 different lambdas, and then do a few operations on account:

std::function<void(int)> credit, debit;
std::function<int(void)> getBalance;

// std::tie to unpack tuple
std::tie(credit,debit,getBalance) = createAccountOps();


std::cout << getBalance();

In the end we call getBalance() to get the account balance. What do you think getBalance() would return?