std::unique_ptr的用法
std::ofstream("demo.txt") << 'x'; // 准备要读的文件
{
std::unique_ptr<std::FILE, decltype(&std::fclose)> fp(std::fopen("demo.txt", "r"),
&std::fclose);
if(fp) // fopen 可以打开失败;该情况下 fp 保有空指针
std::cout << (char)std::fgetc(fp.get()) << '\n';
} // fclose() 调用于此,但仅若 FILE* 不是空指针
// (即 fopen 成功)
自动调用fclose
struct CallHelper {
CallHelper() {
webrtc::AudioState::Config audio_state_config;
audio_state_config.audio_mixer =
new rtc::RefCountedObject<webrtc::test::MockAudioMixer>();
audio_state_config.audio_processing =
new rtc::RefCountedObject<webrtc::test::MockAudioProcessing>();
audio_state_config.audio_device_module =
new rtc::RefCountedObject<webrtc::test::MockAudioDeviceModule>();
webrtc::Call::Config config(&event_log_);
config.audio_state = webrtc::AudioState::Create(audio_state_config);
call_.reset(webrtc::Call::Create(config));
} webrtc::Call* operator->() { return call_.get(); } // 注意此处 private:
webrtc::RtcEventLogNullImpl event_log_;
std::unique_ptr<webrtc::Call> call_;
};
TEST(CallTest, CreateDestroy_AudioSendStream) {
CallHelper call;
AudioSendStream::Config config(nullptr);
config.rtp.ssrc = ;
AudioSendStream* stream = call->CreateAudioSendStream(config); // 注意此处
EXPECT_NE(stream, nullptr);
call->DestroyAudioSendStream(stream);
}
std::unique_ptr的用法的更多相关文章
- 智能指针unique_ptr的用法
unique_ptr是独占型的智能指针,它不允许其他的智能指针共享其内部的指针,不允许通过赋值将一个unique_ptr赋值给另一个unique_ptr,如下面错误用法: std::unique_pt ...
- std::shared_ptr 和 std::weak_ptr的用法以及引用计数的循环引用问题
在std::shared_ptr被引入之前,C++标准库中实现的用于管理资源的智能指针只有std::auto_ptr一个而已.std::auto_ptr的作用非常有限,因为它存在被管理资源的所有权转移 ...
- std::unique_ptr使用incomplete type的报错分析和解决
Pimpl(Pointer to implementation)很多同学都不陌生,但是从原始指针升级到C++11的独占指针std::unique_ptr时,会遇到一个incomplete type的报 ...
- (译+原)std::shared_ptr及std::unique_ptr使用数组
转载请注明出处: http://www.cnblogs.com/darkknightzh/p/5462363.html 参考网址: http://stackoverflow.com/questions ...
- 智能指针std::unique_ptr
std::unique_ptr 1.特性 1) 任意时刻只能由一个unique_ptr指向某个对象,指针销毁时,指向的对象也会被删除(通过内置删除器,通过调用析构函数实现删除对象) 2)禁止拷贝和赋值 ...
- 字符串连接比较(std::unique_ptr实现)
比较代码之间可能相差大,可是速度相差很大,而且目的在于测试unique_ptr使用...; C/C++: #include <iostream> std::unique_ptr<ch ...
- C++11智能指针之std::unique_ptr
C++11智能指针之std::unique_ptr uniqut_ptr是一种对资源具有排他性拥有权的智能指针,即一个对象资源只能同时被一个unique_ptr指向. 一.初始化方式 通过new云 ...
- 智能指针(1)-std::unique_ptr
std::unique_ptr std::unique_ptr是一种几乎和原始指针一样高效的智能指针,对所管理的指针资源拥有独占权.由C++11标准引入,用于替代C++98中过时的std::auto_ ...
- 智能指针思想实践(std::unique_ptr, std::shared_ptr)
1 smart pointer 思想 个人认为smart pointer实际上就是一个对原始指针类型的一个封装类,并对外提供了-> 和 * 两种操作,使得其能够表现出原始指针的操作行为. ...
随机推荐
- vue.config.js常用配置
使用vue-cli3.0搭建项目比之前更简洁,没有了build和config文件夹. vue-cli3的一些服务配置都迁移到CLI Service里面了,对于一些基础配置和一些扩展配置需要在根目录新建 ...
- php查询字符串的函数
/* 查找一个字符串在另一个字符串的第一次出现,并返回其余部分(strstr别名) */ var_dump(strchr("hello world hello", "wo ...
- python函数的基本语法<二>
函数的流程控制: if...else... a = 100 b = 200 if a == 100 and b ==300: print('100,200') elif b == 200: print ...
- MBR分区表的备份与还原
MBR分区表的备份与还原 MBR分区的存储 从下图可以看出,MBR分区前446字节是boot loader:接下来64字节是分区表:再然后就是三个主分区加一个拓展分区. 一.备份分区表,要跳过前446 ...
- CentOS安装图解(在VMware 8上安装)
近期准备学习一下CentOS,打算先在VMware上装一个用于初步了解. 整体步骤如下: 一.获取CentOS安装镜像.可以从以下几个地方获取,顺便提一句,我是从163下载的. 1.CentOS的官方 ...
- go 学习笔记之咬文嚼字带你弄清楚 defer 延迟函数
温故知新不忘延迟基础 A "defer" statement invokes a function whose execution is deferred to the momen ...
- VS安装
1. 只更改工作负载和单个组件 工作负载:我只勾选3个需要的 单个组件: 勾选 .NET 下Framework 别的不用改 2.点击安装,安装完成重启
- git Lab ssh方式拉取代码失败
gitLab在linux上已经安装好了, 在配置项目的时候报如下异常 使用http方式没问题, 但是用ssh方式设置repository URL 提示资源库不存在. returned status c ...
- PHP 向数组头部插入数据
PHP 向数组头部插入数据 函数: array_unshift() 示例: $s = array('a' => 0, 'b' => 3); array_unshift($s, '5'); ...
- Kotlin Coroutines不复杂, 我来帮你理一理
Coroutines 协程 最近在总结Kotlin的一些东西, 发现协程这块确实不容易说清楚. 之前的那篇就写得不好, 所以决定重写. 反复研究了官网文档和各种教程博客, 本篇内容是最基础也最主要的内 ...