Boost.ScopeExit provides the macro BOOST_SCOPE_EXIT, which can be used to define something that looks like a local function but doesn't have a name. However, it does have a parameter list in parentheses and a block in braces.

#include <string>
#include <memory>
#include <cstdio> struct CloseFile {
void operator()(std::FILE* file) {
std::fclose(file);
}
}; void write_to_file(const std::string& s) {
std::unique_ptr<std::FILE, CloseFile> file(std::fopen("hello-world.txt", "a"));
std::fprintf(file.get(), s.c_str());
} int main() {
write_to_file("Hello, ");
write_to_file("world!");
return ;
}

上述代码使用BOOST_SCOPE_EXIT,修改如下:

#include <string>
#include <memory>
#include <cstdio>
#include <boost/scope_exit.hpp> void write_to_file(const std::string& s) {
std::FILE* file = fopen("hello-world.txt", "a");
BOOST_SCOPE_EXIT(&file)
{
std::fclose(file);
} BOOST_SCOPE_EXIT_END
std::fprintf(file, s.c_str());
} int main() {
write_to_file("Hello, ");
write_to_file("world!");
return ;
}

boost scope exit的更多相关文章

  1. Boost简介

    原文链接:  吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...

  2. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  3. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

  4. C++ Boost库分类总结

    c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...

  5. boost 介绍

    简介: Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容 ...

  6. 理解 break, continue, return 和 exit

    你们知道 “break”, “continue”, “return” 和 “exit”的作用吗? 它们是功能强大的语言结构体.下面通过一个测试函数来说明它们之间的不同. 1 2 3 4 5 6 7 8 ...

  7. VS2017 配置 boost_1_70

    1. 下载与安装 1.1 安装方法1 (1) 下载 https://www.boost.org/ 或者使用 https://sourceforge.net/projects/boost/files/b ...

  8. 【C++】《Effective C++》第九章

    杂项讨论 条款53:不要轻忽编译器的警告 请记住 严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取"无任何警告"的容易. 不要过度依赖编译器的报警能力, ...

  9. enote笔记法使用范例(2)——指针(1)智能指针

    要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...

随机推荐

  1. VM安装OSX进度条一半时卡住不动,【附】OSX10.10 ISO镜像文件

    安装OSX10.10真是一波多折,先是下载了一个5G多的原版dmg文件,转成ISO后在虚拟机上无法识别,后按网上的说的方法​​​​在提取出来的BaseSystem.dmg文件,再转成ISO文件,​可以 ...

  2. 使用vue-i18n实现项目的国际化 以及iview的国际化

    一:项目的国际化 vue-i18n官网 1. 在src中新建一个language文件夹(包含index.js.US.js.CN.js) (1)US.js 保存变量的英文,内容: export defa ...

  3. Android逆向之旅---SO(ELF)文件格式详解

    第一.前言 从今天开始我们正式开始Android的逆向之旅,关于逆向的相关知识,想必大家都不陌生了,逆向领域是一个充满挑战和神秘的领域.作为一名Android开发者,每个人都想去探索这个领域,因为一旦 ...

  4. 【前端技术】一篇文章搞掂:微信小程序

    实战: 1.[openId]获取openId 有如下几种方法: 通过wx.login()获取临时登录凭证 code,然后通过code2session获取openId wx.login():https: ...

  5. LintCode之删除链表中的元素

    题目描述 我的代码 /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode n ...

  6. jenkins实现手动选择分支构建项目-Git Paramater

    先下载插件: Git Paramater 参照: jenkins-参数化构建(三)插件:Git Parameter https://www.cnblogs.com/zhaojingyu/p/98624 ...

  7. Powercli随笔 - PowerCLI script to sequentially Storage vMotion VMs from a CSV File

    PowerCLI script to sequentially Storage vMotion VMs from a CSV File This is a PowerCLI script that I ...

  8. django-3-视图(views.py)与网址(urls.py)

    视图与网址 操作文件:urls.py.views.py urls.py 作用:用于处理前台的链接(如前台访问:127.0.0.1:8080/index/1212/21212),其实永远访问的是同一个文 ...

  9. JavaScript Set

    function Set() { var items = {}; this.has = function(value) { return value in items } this.add = fun ...

  10. Springboot集成Mybatis+PageHelper

    1.Springboot项目引入mysql和mybatis的依赖: <dependency> <groupId>org.mybatis.spring.boot</grou ...