std::bad_function_call
来自 cppreference.cn
定义于头文件 <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。参数
其他 | - | 要拷贝的另一个异常对象 |
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。
参数
其他 | - | 用于赋值的另一个异常对象 |
返回值
*this
std::bad_function_call::what
virtual const char* what() const noexcept; |
(C++11 起) | |
返回解释字符串。
返回值
指向一个实现定义的以空字符结尾的字符串的指针,包含解释性信息。该字符串适合转换为 std::wstring 并显示。该指针保证在获取它的异常对象被销毁之前,或者在异常对象上调用非常量成员函数(例如复制赋值运算符)之前一直有效。
在常量求值期间,返回的字符串使用普通字面量编码进行编码。 |
(C++26 起) |
注意
允许但不要求实现重写 what()
。
继承自 std::exception
成员函数
[virtual] |
销毁异常对象 ( std::exception 的虚公共成员函数) |
[virtual] |
返回解释字符串 ( 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++ 标准。
缺陷报告 | 应用于 | 发布时的行为 | 正确的行为 |
---|---|---|---|
LWG 2233 | C++11 | what() 总是返回与 std::exception::what() 相同的解释性字符串字符串作为 std::exception::what() |
返回其自身的 解释性字符串 |
[编辑] 另请参阅
(C++11) |
任何可复制构造的可调用对象的包装器 (类模板) |