[英]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. 我见过几个 问题是指
std::bad_function_call
例外 ,但一直没能谷歌搜索是什么原因导致这个例外,找出任何。
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?
你能给我一些没有其他语义问题的最小例子吗?
Sure- the easiest is where you try to call a std::function
that's empty. 当然 - 最简单的是你尝试调用一个空的
std::function
。
int main() {
std::function<int()> intfunc;
int x = intfunc(); // BAD
}
"Performing a function call without having a target to call throws an exception of type std::bad_function_call" “在没有调用目标的情况下执行函数调用会抛出类型为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 我没有信用....它的C ++标准Lib的Nicolai Josuttis Pundit
in my case was the problem was in capture list. 在我的情况下,问题是在捕获列表中。 i have a recursive lambda function.
我有一个递归的lambda函数。
//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. 从捕获列表中的f_build中丢失并生成错误的调用。
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). 但这取决于编译器(vc ++ throws,g ++ not)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.