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. 命令行启动appium服务

    Android终端 appium --avd test -a 127.0.0.1 -p 4723 --language "zh_CN" --locale "CN" ...

  2. [转]解决win10下localhost打不开的问题

    博主刚开始玩Tornado,结果localhost都打不开,各种找寻解决方案,结论都是IIS服务器问题.然而win10下的解决方法居然没人写过...那就我来配图详解下. 打开控制面板--添加或删除程序 ...

  3. vfs之mount()

    首先明确一点,mount是vfs层的操作. 它的核心是从设备(可能是一个分区)上读出一个super block,把这个分区对应的文件系统的vfs函数表注册到super block的sb_opearti ...

  4. boost multi array

    Boost MultiArray is a library that simplifies using arrays with multiple dimensions. 1. #include < ...

  5. c++ 递归思想 阶乘

    #include "stdio.h" #include "iostream" long fact(int n); int main() { int i; sca ...

  6. php简易分词

    http://www.xunsearch.com/ 示例 http://www.xunsearch.com/scws/demo/v48.php

  7. SSH弱小算法支持问题

    SSH弱小算法支持问题:SSH的配置文件中加密算法没有指定(没有配置加密算法),则会默认支持所有加密算法,包括arcfour,arcfour128,arcfour256等弱加密算法.解决方法:1.修改 ...

  8. 听说你懂个J?——前端发展闲聊

    刚好周末和朋友聊起"前端从受鄙视到变得重要"这个话题,感慨前端这四年来的发展,遂有本文. 1. 前情提要 毋庸讳言,在我刚工作的时候,前端是还是一个不受重视的岗位.切图狗,写网页的 ...

  9. Spring Boot学习一之配置类及自动配置

    一.配置类 1. 导入其他配置类 你不需要将所有的 @Configuration 放进一个单独的类, @Import 注解可以用来导入其他配置类.另外,你也可以使用 @ComponentScan 注解 ...

  10. linux下不同服务器间数据传输(rcp,scp,rsync,ftp,sftp,lftp,wget,curl)

    因为工作原因,需要经常在不同的服务器见进行文件传输,特别是大文件的传输,因此对linux下不同服务器间数据传输命令和工具进行了研究和总结.主要是rcp,scp,rsync,ftp,sftp,lftp, ...