Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What causes std::bad_function_call?

I've seen a few questions that refer to the std::bad_function_call exception, but haven't been able to find out any by Googling about what causes this exception.

What kind of behavior is supposed to cause this exception? Can you give me minimal examples that don't have other semantic problems also going on?

like image 972
Ken Bloom Avatar asked Apr 06 '11 13:04

Ken Bloom


3 Answers

Sure- the easiest is where you try to call a std::function that's empty.

int main() {
    std::function<int()> intfunc;
    int x = intfunc(); // BAD
}
like image 51
Puppy Avatar answered Nov 10 '22 20:11

Puppy


In my case, the problem was in the capture list. I have a recursive lambda function.

//decl
std::function<void(const SBone*, const core::vector3df&, const core::quaternion&)> f_build;
f_build = [&f_build](const SBone* bone, const core::vector3df& pos, const core::quaternion& rot)
{
...
}

Missing & from f_build in the capture list generates a bad call.

like image 5
Corneliu Bratu Avatar answered Nov 10 '22 22:11

Corneliu Bratu


"Performing a function call without having a target to call throws an exception of type std::bad_function_call"

    std::function<void(int,int)> f;
    f(33,66); // throws std::bad_function_call

No credits to me....its Nicolai Josuttis Pundit of C++ Standard Lib

like image 4
shraddha_sinha Avatar answered Nov 10 '22 22:11

shraddha_sinha