1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. A std::function f wraps a lambda that captures a large…

Added 30 Nov -0001

A std::function<void()> f wraps a lambda that captures a large array by value. What is the practical consequence?

  1. The capture is moved to the heap once and shared between copies, making copies cheap
  2. std::function rejects capturing lambdas, so the code fails to compile
  3. The closure object lives inside f, so copying f copies the whole captured array Correct answer
  4. The lambda decays to a plain function pointer, so the captured array is not kept alive

Correct answer

C. The closure object lives inside f, so copying f copies the whole captured array

14 views

MCQSEXAM

Explanation

std::function uses type erasure and stores the callable either in a small inline buffer or on the heap, so its own size is fixed but the closure's data travels with it. Copying the function object copies the closure unless the state was captured through a shared_ptr. On hot paths a template parameter or a reference wrapper avoids the indirect call and the possible allocation.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is C? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs