std::bad_function_call
来自 cppreference.cn
< 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) 默认构造函数。
2) 复制构造函数。 如果 *this 和 other 都具有动态类型
std::bad_function_call
,则 std::strcmp(what(), other.what()) == 0。参数
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
virtual const char* what() const noexcept; |
(自 C++11 起) | |
返回解释性字符串。
返回值
指向实现定义的空终止字符串的指针,其中包含解释性信息。 该字符串适合转换为 std::wstring 并显示。 指针保证有效,至少到获取它的异常对象被销毁,或者直到在异常对象上调用非 const 成员函数(例如,复制赋值运算符)。
返回的字符串使用常量求值期间的普通字面量编码进行编码。 |
(自 C++26 起) |
注释
允许但不是必须实现 what()
的重写。
继承自 std::exception
成员函数
[虚拟] |
销毁异常对象 ( std::exception 的虚拟公有成员函数) |
[虚拟] |
返回解释性字符串 ( std::exception 的虚拟公有成员函数) |
[编辑] 示例
运行此代码
#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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 2233 | C++11 | what() 始终返回相同的解释性字符串,如 std::exception::what() |
返回自己的 解释性字符串 |
[编辑] 参见
(C++11) |
任何可复制构造的可调用对象的可复制包装器 (类模板) |