名稱空間
變體
操作

std::bad_function_call

來自 cppreference.com
 
 
 
函式物件
函式包裝器
(C++11)
(C++11)
bad_function_call
(C++11)
函式呼叫
(C++17)(C++23)
恆等函式物件
(C++20)
透明運算子包裝器
(C++14)
(C++14)
(C++14)
(C++14)  
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)
(C++14)

舊繫結器和介面卡
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)
(直到 C++17*)  
(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
(直到 C++17*)(直到 C++17*)
(直到 C++17*)(直到 C++17*)

(直到 C++17*)
(直到 C++17*)(直到 C++17*)(直到 C++17*)(直到 C++17*)
(直到 C++20*)
(直到 C++20*)
 
定義於標頭檔案 <functional>
class bad_function_call;

std::bad_function_callstd::function::operator() 在函式包裝器沒有目標時丟擲的異常型別。

cpp/error/exceptionstd-bad function call-inheritance.svg

繼承圖

目錄

[編輯] 成員函式

(建構函式)
構造一個新的 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) 複製建構函式。如果 *thisother 都具有動態型別 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 的內容。如果 *thisother 都具有動態型別 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

成員函式

銷燬異常物件
(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)
任何可複製構造的可呼叫物件的包裝器
(類模板) [編輯]