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 ...
随机推荐
- [转]Win10企业版无法访问共享文件夹
Win10系统电脑在更新后,当我们访问共享文件夹时可能会出现如下图所示窗口,导致我们无法访问.那么这个问题如何解决呢?具体如下:1. 首先我们按“Windows+R键”打开运行窗口.2. 在该窗口文本 ...
- 如何在pycharm中进入shell脚本调试代码
首先在Teramal终端 输入python manage.py shell 然后进行下图操作来调试代码
- 看图了解RocksDB
它是一个高性能的Key-Value数据库.设计了完善的持久化机制,同时保证性能和安全性.能够良好的支持范围查询,因为K-V记录就是按照Key来排序的. 下图为写入的流程: 可以看到主要的三个组成部 ...
- python中常用得字符串,列表函数汇总
字符串函数: 1,replace函数,替换函数.s = s.replace(old,new),老得元素被新的元素替换.注意不能直接写s.replace(old,new).要写s=s.replace(o ...
- ANTLR4在windows10下的安装
1.下载ANTLR ①.从官网下载到最新版本的antlr-4.7.1-complete.jar.我下载的时候最新版本是4.7.1. ②.选择路径保存,为方便之后修改环境变量.我的下载目录为E:\Ant ...
- QTP学习笔记--Excel数据源
直接读取Excel表格的function摘自此处http://www.51testing.com/html/40/307440-827863.html 特此感谢! Excel作为QTP自动化测试的数 ...
- AIZU 2560 [想法题]
表示敲完多项式乘法&高精度乘法两道FFT模板题后就开始来磕这题了 这题相对而言应该不算模板题 不过神犇们肯定还是一眼看穿 如果原OJ访问速度较慢的话可以考虑戳这里 http://acm.hus ...
- 104、Tensorflow 的变量重用
import tensorflow as tf # 在不同的变量域中调用conv_relu,并且声明我们想创建新的变量 def my_image_filter(input_images): with ...
- git 裸库
初始化一个空的裸仓库 $ cd /home/repo $ mkdir tproject.git $ cd tproject.git $ git init - -bare 注:这是在服务器上运 ...
- scrapy爬虫值Items
Items有哪些知识? 1.声明 import scrapy class Product(scrapy.Item): name = scrapy.Field() price = scrapy.Fiel ...