An empty std::function<void()> is invoked as f(). What occurs at run time?
Correct answer
C. std::bad_function_call is thrown, because no callable target is stored
20 views
C. std::bad_function_call is thrown, because no callable target is stored
20 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is C? Ask here and get an explanation.