[英]What causes std::bad_function_call?
我見過幾個 問題是指std::bad_function_call
例外 ,但一直沒能谷歌搜索是什么原因導致這個例外,找出任何。
什么樣的行為應該導致這個例外? 你能給我一些沒有其他語義問題的最小例子嗎?
當然 - 最簡單的是你嘗試調用一個空的std::function
。
int main() {
std::function<int()> intfunc;
int x = intfunc(); // BAD
}
“在沒有調用目標的情況下執行函數調用會拋出類型為std :: bad_function_call的異常”
std::function<void(int,int)> f;
f(33,66); // throws std::bad_function_call
我沒有信用....它的C ++標准Lib的Nicolai Josuttis Pundit
在我的情況下,問題是在捕獲列表中。 我有一個遞歸的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)
{
...
}
從捕獲列表中的f_build中丟失並生成錯誤的調用。
調用臨時函數也可以拋出:
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;
}
但這取決於編譯器(vc ++ throws,g ++ not)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.