47

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?

Community
  • 1
  • 1
Ken Bloom
  • 57,498
  • 14
  • 111
  • 168

4 Answers4

64

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
}
Puppy
  • 144,682
  • 38
  • 256
  • 465
  • 1
    I just did a search and not only is your example the easiest, it also looks like the *only* example! :-) – Howard Hinnant Apr 06 '11 at 14:05
  • 1
    @Howard: Really? Then how does [this answer](http://stackoverflow.com/questions/5556183/make-c-crash-without-casting/5557843#5557843) throw a `bad_function_call`? – Ken Bloom Apr 06 '11 at 16:15
  • 4
    My best guess is that your referenced answer boils down to the same case as given by DeadMG's answer here: calling an empty `std::function`. Sorry, I can't currently test lambda code. I double checked the latest lambda spec and I can find nothing there that throws `bad_function_call`. I re-searched the latest draft for `bad_function_call`, taking care to catch cases where `bad_function_call` might be hyphenated, and did not find any other cases where it is thrown. Still it is possible I missed one. If you find it, please post a pointer to it for us. – Howard Hinnant Apr 06 '11 at 16:45
  • @Howard: after thinking about it, I think that what's going on in that question is that some sort of wierd memory corruption (caused by the dangling reference) is making one of the lambdas look like it's been default-constructed. In particular, I think that the vector's pointer to its internal storage may be the memory that's getting corrputed (either that or some iterator used locally in the `for_each`). – Ken Bloom Apr 08 '11 at 20:20
  • Sounds like a solid theory to me. – Howard Hinnant Apr 08 '11 at 20:48
  • 4
    Can't believe that it's been so long but I didn't notice that the signature of the function was wrong, rofl. – Puppy Feb 20 '14 at 13:38
5

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49
4

"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

Rudi
  • 19,366
  • 3
  • 55
  • 77
shraddha_sinha
  • 111
  • 1
  • 1
  • 1
    The stack exchange isnt taking the "<>" enclosed code! The content inside "<>" code isn't visible. Before u stone me to death....ahem... std::function f; f(33,66); // throws std::bad_function_call – shraddha_sinha Sep 18 '14 at 11:44
3

Call of a temporary function also can throw:

struct F
{
    const std::function<void()>& myF;

    F(const std::function<void()>& f) : myF(f) {}

    void call()
    {
        myF();
    }
};

int main()
{
    F f([]{ std::cout << "Hello World!" << std::endl;});

    f.call();

    return 0;
}

But this depend on compiler (vc++ throws, g++ not).

Tobias Wollgam
  • 761
  • 2
  • 8
  • 25