命名空间
变体
操作

std::_Exit

来自 cppreference.cn
< cpp‎ | utility‎ | program
 
 
 
 
在头文件 <cstdlib> 中定义
[[noreturn]] void _Exit( int exit_code ) noexcept;
(自 C++11 起)

导致程序正常终止,但不会完全清理资源。

不调用具有自动、线程局部和静态存储持续时间的变量的析构函数。不调用传递给 std::at_quick_exit()std::atexit() 的函数。是否关闭打开的资源(如文件)是实现定义的。

如果 exit_code0EXIT_SUCCESS,则向宿主环境返回一个实现定义的、指示成功终止的状态。如果 exit_codeEXIT_FAILURE,则返回一个实现定义的、指示不成功终止的状态。在其他情况下,返回实现定义的状态值。

自由实现需要提供 std::_Exit

(自 C++23 起)

目录

[编辑] 参数

exit_code - 程序的退出状态

[编辑] 返回值

(无)

[编辑] 注释

尽管自 C++23 起 _Exit 需要是独立的,但在独立的 C 实现中不一定可用。

[编辑] 示例

#include <iostream>
 
class Static
{
public:
    ~Static() 
    {
        std::cout << "Static dtor\n";
    }
};
 
class Local
{
public:
    ~Local() 
    {
        std::cout << "Local dtor\n";
    }
};
 
Static static_variable; // dtor of this object will *not* be called
 
void atexit_handler()
{
    std::cout << "atexit handler\n";
}
 
int main()
{
    Local local_variable; // dtor of this object will *not* be called
 
    // handler will *not* be called
    const int result = std::atexit(atexit_handler);
 
    if (result != 0)
    {
        std::cerr << "atexit registration failed\n";
        return EXIT_FAILURE;
    }
 
    std::cout << "test" << std::endl; // flush from std::endl
        // needs to be here, otherwise nothing will be printed
    std::_Exit(EXIT_FAILURE);
}

输出

test

[编辑] 参见

导致程序异常终止(不清理)
(函数) [编辑]
导致程序正常终止并清理
(函数) [编辑]
C 文档 for _Exit