命名空间
变体
操作

std::_Exit

来自 cppreference.cn
< cpp‎ | 实用工具‎ | 程序
 
 
 
 
定义于头文件 <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 文档 用于 _Exit