什么是C ++标准延迟/终结执行? [英] What is standard defer/finalizer implementation in C++?

查看:367
本文介绍了什么是C ++标准延迟/终结执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

的Golang风格的延迟解释的这里和的这里

General idea of Golang-style defer is explained here and here.

我不知道,确实STL(C ++的11,C ++ 14,...)或者升压或者一些其他图书馆实现包含这样一类?所以,我可以只使用它,而在每一个新项目重新实现它。

I wonder, does STL (of C++11, C++14, ...) or maybe Boost or maybe some other library contain implementation of such a class? So I could just use it without reimplementing it in every new project.

推荐答案

有一个的提案的std :: unique_resource_t 这将使code像

There is a proposal for std::unique_resource_t which will enable code like

auto file=make_unique_resource(::fopen(filename.c_str(),"w"),&::fclose);

有关资源,它定义了一个一般 scope_exit ,这应该是一样的延迟

for resources, and it defines a general scope_exit, which should be the same as defer:

// Always say goodbye before returning,
auto goodbye = make_scope_exit([&out]() ->void
{
out << "Goodbye world..." << std::endl;
});

<一个href=\"http://stackoverflow.com/questions/30405269/will-there-be-standardization-of-scope-guard-scope-exit-idioms\">It看起来将是下一个标准的一部分。

这篇关于什么是C ++标准延迟/终结执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆