A std::function<void()> f wraps a lambda that captures a large array by value. What is the practical consequence?
Correct answer
C. The closure object lives inside f, so copying f copies the whole captured array
14 views
C. The closure object lives inside f, so copying f copies the whole captured array
14 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.