博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C++ auto_ptr
阅读量:7112 次
发布时间:2019-06-28

本文共 2021 字,大约阅读时间需要 6 分钟。

虽然auto_ptr已经过时了, 但是对这种古董做考古工作仍旧有很多裨益。

#include 
#include
using namespace std;struct S{ ~S(){ std::cout << "~S() called" << std::endl; }};void foo( std::auto_ptr
ptr){ //ptr.release();}int main () { std::auto_ptr ptr(new S); foo(ptr); if (!ptr.get()){ std::cout << "ptr is null" ; }}

所有权移交(总是move),是auto_ptr的核心语义之一。当调用foo(ptr)后,会打印输出“ptr is null” 。

为了防止发生foo中的行为,可以定成:

1 void foo( const std::auto_ptr& other){2      //other.release(); //compile error。 const& ptr 阻止了对ptr.release(). 3 }

那么auto_ptr中的拷贝构造函数是什么样子的?

auto_ptr( const auto_ptr& other);     //这绝不可能,为了实现唯一所有权,我们需要对other.release(); 显然const阻止了这个事情的发生。

auto_ptr( auto_ptr& other);   //正确的。

那么这又怎么解释?

std::auto_ptr makeS(){    std::auto_ptr ret();        return ret;}std::auto_ptr a = makeS();

C++03时代,makeS()返回右值,右值只能传递给auto_ptr(const auto_ptr& other);或者 auto_ptr( auto_ptr&& other); 可惜这两个函数都不存在。

解决办法就是:auto_ptr_ref. 本质是个抓右值的工具(在C++03时代,是个开创性的发明)。右值std::auto_ptr<S>隐式转换成auto_ptr_ref<S>, 传递给template< class Y > auto_ptr( auto_ptr_ref<Y> m );

下面是模拟,先提出一个问题,是C++03无法编译的:

#include 
using namespace std;struct S{ S(int i){ std::cout << "S(int i) called \n"; i_in_s_ = i; } S(S &s){ std::cout << "S(S s) called \n"; } int i_in_s_;};int main(){ S b = S(1);}

还是那个T& 无法绑定右值的问题,解决如下:

#include 
using namespace std;struct S{ struct S_ref{ S_ref(int i): i_(i){ } int i_; }; operator S_ref(){ std::cout << "operator S_ref called \n"; return S_ref(i_in_s_); } S(int i){ std::cout << "S(int i) called \n"; i_in_s_ = i; } S(S &s){ std::cout << "S(S s) called \n"; } S(S_ref r){ std::cout << "S(S_ref r) called \n"; i_in_s_ = r.i_; } int i_in_s_;};int main(){ S b = S(1);}

解决。思想就是:抓右值的目的,无非就是想拷贝点东西出来吧。我先把东西拷贝到S_ref,然后再传给S(S_ref r)。也能实现传递数据的功能。

考古工作暂时告一段落。

转载于:https://www.cnblogs.com/thomas76/p/8647694.html

你可能感兴趣的文章
kettle删除资源库中的转换或者作业
查看>>
java的重写规则
查看>>
Base64编码原理与应用
查看>>
物联网产业链八大环节全景图
查看>>
spark dataframe操作集锦(提取前几行,合并,入库等)
查看>>
阿里巴巴
查看>>
伺服电机的调试步骤有哪些
查看>>
shell中的四种模式匹配
查看>>
(转)Making 1 million requests with python-aiohttp
查看>>
Web应用扫描工具Wapiti
查看>>
hadoop-17-hive数据库元数据查看
查看>>
Bind-DLZ with MySQL
查看>>
Google 地图切片URL地址解析
查看>>
angualrjs 配置超时时间
查看>>
Nvme固体硬盘Intel750,SM961分别使用一段时间以后对比
查看>>
基于JMH的Benchmark解决方案
查看>>
IEWebcontrol webctrl_client目录配置
查看>>
SQL SERVER 2014 Agent服务异常停止案例
查看>>
linux文件打包tar.gz的命令
查看>>
《Office 365 开发入门指南》公开邀请试读,欢迎反馈
查看>>