boost scope exit
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的更多相关文章
- Boost简介
原文链接: 吴豆豆http://www.cnblogs.com/gdutbean/archive/2012/03/30/2425201.html Boost库 Boost库是为C++语言标准库提供扩 ...
- Boost 1.61.0 Library Documentation
http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...
- boost库的安装,使用,介绍,库分类
1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...
- C++ Boost库分类总结
c# 程序员写c++,各种不适应.尤其是被内存操作和几十种字符串类型的转换,简直疯了,大小写转换竟然要手动写代码实现. Boost看介绍不错,也不知道能不能跨平台.过几天要上linux写c++, 也不 ...
- boost 介绍
简介: Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的开发引擎之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容 ...
- 理解 break, continue, return 和 exit
你们知道 “break”, “continue”, “return” 和 “exit”的作用吗? 它们是功能强大的语言结构体.下面通过一个测试函数来说明它们之间的不同. 1 2 3 4 5 6 7 8 ...
- VS2017 配置 boost_1_70
1. 下载与安装 1.1 安装方法1 (1) 下载 https://www.boost.org/ 或者使用 https://sourceforge.net/projects/boost/files/b ...
- 【C++】《Effective C++》第九章
杂项讨论 条款53:不要轻忽编译器的警告 请记住 严肃对待编译器发出的警告信息.努力在你的编译器的最高(最严苛)警告级别下争取"无任何警告"的容易. 不要过度依赖编译器的报警能力, ...
- enote笔记法使用范例(2)——指针(1)智能指针
要知道什么是智能指针,首先了解什么称为 “资源分配即初始化” what RAII:RAII—Resource Acquisition Is Initialization,即“资源分配即初始化” 在&l ...
随机推荐
- boost propertyTree
Boost PropertyTree provides a tree structure to store key/value pairs. Tree structures means that a ...
- 报错——userdel: user hhh is currently used by process 9218
报错 userdel: user hhh is currently used by process 9218 [root@centos71 ~]# useradd hhh [root@centos71 ...
- UITableViewCell的移动
看到Metro大都会 这个App中扣款顺序有个cell可以移动,于是觉得是时候回忆一下UITableView的基本使用了.其实他这个移动cell的功能是系统自带的. 代码主要是这样: // // Vi ...
- 【BZOJ3473&BZOJ3277】字符串(广义后缀自动机)
题意:给定n个字符串,询问每个字符串有多少子串(不包括空串)是所有n个字符串中至少k个字符串的子串? 本质相同的子串算多个. 对于 100% 的数据,1<=n,k<=10^5,所有字符串总 ...
- window安装nodejs
nvm管理nodejs 原文: https://www.cnblogs.com/shimily/articles/7244058.html1.下载nvm(nodejs版本管理工具) https://g ...
- js中的$符
js中的$代表什么意思呢? 首先js的作用是什么呢?是修饰网页动态内容的.那么修饰就需要定位主题,比如你把html比喻一个美女,让她唱一首歌.那么首先你要定位出是你想让哪个美女唱歌,通常我们用id来定 ...
- (四)添加yaffs2文件系统支持
1. 获取yaffs2源码 在linux工作目录下进行clone操作: git clone git://www.aleph1.co.uk/yaffs2 完成后会在当前目录下产生yaffs2的源码目录: ...
- 显示等待WebDriverWait+lambda
代码,关键代码标红 参考文章:https://www.cnblogs.com/yoyoketang/p/6517477.html #coding:utf-8 ''' 这里写了一个百度搜索页的pageo ...
- [题解]Print a 1337-string...-数学(codeforces 1202D)
题目链接:https://codeforces.com/problemset/problem/1202/D 题意: 构造一串只由 ‘1’,‘3’,‘7’ 组成的字符串,使其 ‘1337’ 子序列数量为 ...
- 数组中重复的数字(js实现)
题目 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为7的 ...