Namespaces
Variants

std:: bad_function_call

From cppreference.net
Utilities library
Function objects
Function wrappers
(C++11)
(C++11)
bad_function_call
(C++11)
Function invocation
(C++17) (C++23)
Identity function object
(C++20)
Old binders and adaptors
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
( until C++17* ) ( until C++17* )
( until C++17* ) ( until C++17* )

( until C++17* )
( until C++17* ) ( until C++17* ) ( until C++17* ) ( until C++17* )
( until C++20* )
( until C++20* )
定义于头文件 <functional>
class bad_function_call ;

std::bad_function_call 是当函数包装器没有目标时,由 std::function::operator() 抛出的异常类型。

cpp/error/exception std-bad function call-inheritance.svg

继承关系图

目录

成员函数

(constructor)
构造新的 bad_function_call 对象
(公开成员函数)
operator=
替换 bad_function_call 对象
(公开成员函数)
what
返回说明性字符串
(公开成员函数)

std::bad_function_call:: bad_function_call

bad_function_call ( ) noexcept ;
(1) (since C++11)
bad_function_call ( const bad_function_call & other ) noexcept ;
(2) (since 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 ;
(since 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 ;
(since C++11)

返回解释性字符串。

返回值

指向实现定义的以空字符结尾的解释性字符串的指针。该字符串适合转换并显示为 std::wstring 。该指针保证至少在获取它的异常对象被销毁之前,或者在调用异常对象上的非const成员函数(例如拷贝赋值运算符)之前保持有效。

注解

实现允许但不要求重写 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() 相同的说明性字符串
返回其自身的
说明性字符串

参见

(C++11)
任何可复制构造的可调用对象的可复制包装器
(类模板)