std::bad_function_call
来自cppreference.com
< cpp | utility | functional
在标头 <functional> 定义 | ||
class bad_function_call; | ||
std::bad_function_call
是当函数包装器无目标时 std::function::operator() 所抛出的异常类型。
目录 |
[编辑]成员函数
(构造函数) | 构造新的 bad_function_call 对象 (公开成员函数) |
operator= | 替换 bad_function_call 对象 (公开成员函数) |
what | 返回解释字符串 (公开成员函数) |
std::bad_function_call::bad_function_call
bad_function_call()noexcept; | (1) | (C++11 起) |
bad_function_call(const bad_function_call& other )noexcept; | (2) | (C++11 起) |
构造新的拥有实现定义的空终止字节字符串的 bad_function_call
对象,字符串能通过 what() 访问。
1) 默认构造函数。
参数
other | - | 要复制的另一异常对象 |
std::bad_function_call::operator=
bad_function_call& operator=(const bad_function_call& other )noexcept; | (C++11 起) | |
以 other 的内容赋值。如果 *this 与 other 均拥有动态类型 std::bad_function_call
,那么赋值后 std::strcmp(what(), other.what())==0。
参数
other | - | 用来赋值的另一异常对象 |
返回值
*this
std::bad_function_call::what
virtualconstchar* what()constnoexcept; | (C++11 起) | |
返回解释字符串。
返回值
指向有由实现定义的解释信息的空终止字符串的指针。该字符串适合转换并显示为 std::wstring。保证该指针至少直到获得它来源的异常对象被销毁,或在该异常对象上调用非 const 成员函数(例如复制赋值运算符)为止一直有效。
返回的字符串在常量求值过程中按普通字面量编码。 | (C++26 起) |
注解
允许但不要求实现覆写 what()
。
[编辑]示例
运行此代码
#include <functional>#include <iostream> int main(){std::function<int()> f = nullptr;try{ f();}catch(const std::bad_function_call& e){std::cout<< e.what()<<'\n';}}
可能的输出:
bad function call
[编辑]缺陷报告
下列更改行为的缺陷报告追溯地应用于以前出版的 C++ 标准。
缺陷报告 | 应用于 | 出版时的行为 | 正确行为 |
---|---|---|---|
LWG 2233 | C++11 | what() 始终会返回与 std::exception::what() 相同的解释字符串 | 返回自己的解释字符串 |
[编辑]参阅
(C++11) | 任意可复制构造的可调用对象的可复制包装 (类模板) |