What causes std::bad_function_call?

后端 未结 4 534
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-30 19:41

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.

W

相关标签:
4条回答
  • 2020-12-30 19:46

    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).

    0 讨论(0)
  • 2020-12-30 19:52

    in my case was the problem was in 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 capture list generate a bad call.

    0 讨论(0)
  • 2020-12-30 20:01

    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
    }
    
    0 讨论(0)
  • 2020-12-30 20:02

    "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

    0 讨论(0)
提交回复
热议问题