1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. An empty std::function is invoked as f(). What occurs at…

Added 30 Nov -0001

An empty std::function<void()> is invoked as f(). What occurs at run time?

  1. A silent no-op, because the call operator checks for an empty target
  2. A segmentation fault, since the internal pointer is null and dereferenced
  3. std::bad_function_call is thrown, because no callable target is stored Correct answer
  4. Undefined behaviour, because std::function cannot be empty when called

Correct answer

C. std::bad_function_call is thrown, because no callable target is stored

20 views

MCQSEXAM

Explanation

An empty std::function throws std::bad_function_call from its call operator rather than crashing, and the same happens when the stored target is a null function pointer. Test with if (f) or the explicit bool conversion before calling. Unlike a raw function pointer, std::function has a defined empty state and defined behaviour when it is misused.

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