Which C++ feature provides a concise way to define an unnamed function object?

auto square = [](int x)
{
    return x * x;
};

cout << square(5);
                        

Options

  • A. Lambda expression
  • B. Preprocessor directive
  • C. Namespace
  • D. More than one of the above
  • E. None of the above

Correct Answer (Detailed Explanation is Below)

A. Lambda expression

Detailed Explanation

Answer: (A) Lambda expression

A lambda expression provides a convenient way to create an unnamed function directly where it is needed.

In the example, [] introduces the lambda expression and (int x) specifies its parameter.

The lambda returns x * x. Therefore, calling square(5) produces 25.