Q:

Write a lambda that captures an int from its enclosing function and takes an int parameter. The lambda should return the sum of the captured int and the int parameter

0

Write a lambda that captures an int from its enclosing function and takes an int parameter. The lambda should return the sum of the captured int and the int parameter.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <iostream>

int main() {
  int x, y;
  std::cin >> x >> y;
  auto sum = [x](int i) { return x + i; };
  // The lamda must be defined after we read x, otherwise it will capture the
  // undefined value of x.
  std::cout << x << " + " << y << " = " << sum(y) << std::endl;

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now