std::longjmp
來自 cppreference.com
定義於標頭檔案 <csetjmp> |
||
void longjmp( std::jmp_buf env, int status ); |
(C++17 前) | |
[[noreturn]] void longjmp( std::jmp_buf env, int status ); |
(C++17 起) | |
載入透過先前呼叫 setjmp 儲存的執行上下文 env。此函式不會返回。控制被轉移到設定 env 的宏 setjmp 的呼叫點。該 setjmp 然後返回作為 status 傳遞的值。
如果呼叫 setjmp 的函式已退出,則行為未定義(換句話說,只允許向上呼叫堆疊的長期跳轉)。
目錄 |
[編輯] C++ 中的額外限制
除了 C 的 longjmp,C++ 的 std::longjmp
具有更受限制的行為。
如果用 throw 替換 std::longjmp
,用 catch 替換 setjmp 會為任何自動物件呼叫 非平凡的解構函式,則此 std::longjmp
的行為未定義。
如果在協程中,在可以使用 co_await 運算子的地方呼叫 |
(C++20 起) |
[編輯] 引數
env | - | 指向由 setjmp 儲存的程式執行狀態的變數 |
status | - | 從 setjmp 返回的值。如果它等於 0,則改用 1 |
[編輯] 返回值
(無)
[編輯] 注意
std::longjmp
是 C 中用於處理函式無法有意義地返回的意外錯誤情況的機制。C++ 通常為此目的使用異常處理。
[編輯] 示例
執行此程式碼
#include <array> #include <cmath> #include <csetjmp> #include <cstdlib> #include <format> #include <iostream> std::jmp_buf solver_error_handler; std::array<double, 2> solve_quadratic_equation(double a, double b, double c) { const double discriminant = b * b - 4.0 * a * c; if (discriminant < 0) std::longjmp(solver_error_handler, true); // Go to error handler const double delta = std::sqrt(discriminant) / (2.0 * a); const double argmin = -b / (2.0 * a); return {argmin - delta, argmin + delta}; } void show_quadratic_equation_solution(double a, double b, double c) { std::cout << std::format("Solving {}x² + {}x + {} = 0...\n", a, b, c); auto [x_0, x_1] = solve_quadratic_equation(a, b, c); std::cout << std::format("x₁ = {}, x₂ = {}\n\n", x_0, x_1); } int main() { if (setjmp(solver_error_handler)) { // Error handler for solver std::cout << "No real solution\n"; return EXIT_FAILURE; } for (auto [a, b, c] : {std::array{1, -3, 2}, {2, -3, -2}, {1, 2, 3}}) show_quadratic_equation_solution(a, b, c); return EXIT_SUCCESS; }
輸出
Solving 1x² + -3x + 2 = 0... x₁ = 1, x₂ = 2 Solving 2x² + -3x + -2 = 0... x₁ = -0.5, x₂ = 2 Solving 1x² + 2x + 3 = 0... No real solution
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 619 | C++98 | C++ 中額外限制的措辭模糊 | 改進了措辭 |
LWG 894 | C++98 | 如果用 throw 替換std::longjmp ,用 catch 替換setjmp 會銷燬任何自動物件,則行為未定義 |
行為僅在以下情況下未定義: 為任何自動物件呼叫 非平凡的解構函式 |
[編輯] 參閱
儲存上下文 (函式宏) | |
C 文件中關於 longjmp 的內容
|