C++11 关于shared_ptr的用法

偏爱风流 2014-01-13 04:19:41
一些关于shared_ptr大部分也只阐述了其最基本的用法。
但当有大量的指针需要new时不免也会产生过多的碎片,自定义分配器是一个好的解决方法。
参考了下C++11的标准:
template<class Y, class D> shared_ptr(Y* p, D d);
template<class Y, class D, class A> shared_ptr(Y* p, D d, A a);
template <class D> shared_ptr(nullptr_t p, D d);
template <class D, class A> shared_ptr(nullptr_t p, D d, A a);
8 Requires: p shall be convertible to T*. D shall be CopyConstructible. The copy constructor and
destructor of D shall not throw exceptions. The expression d(p) shall be well formed, shall have
well defined behavior, and shall not throw exceptions. A shall be an allocator (17.6.3.5). The copy
constructor and destructor of A shall not throw exceptions.
9 Effects: Constructs a shared_ptr object that owns the object p and the deleter d. The second and
fourth constructors shall use a copy of a to allocate memory for internal use.
10 Postconditions: use_count() == 1 && get() == p.
11 Throws: bad_alloc, or an implementation-defined exception when a resource other than memory
could not be obtained.
12 Exception safety: If an exception is thrown, d(p) is called.

依着理解应该是这么回事:

template<typename T>
class M_Alloc
{
//TODO
//对分配器的要求没有细看,跳转的太多了。
};
template<typename T>
class M_Delete
{
//TODO
};
int* p = nullptr;
M_Delete d;
M_Alloc a;
std::shared_ptr<int,M_Delete,M_Alloc>(p,d,a);

不知道如此使用是否正确。
PS:boost::shared_ptr 没有用过
...全文
234 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
偏爱风流 2014-01-13
  • 打赏
  • 举报
回复
引用 5 楼 ganpengjin1 的回复:
[quote=引用 4 楼 lx458004975 的回复:] [quote=引用 3 楼 ganpengjin1 的回复:] [quote=引用 2 楼 lx458004975 的回复:] [quote=引用 1 楼 ganpengjin1 的回复:] 恩,处理分配器,这个得小心使。
水区经常看到你哈。 分配器是有点麻烦,A shall be an allocator (17.6.3.5). 这一跳过去都是表,都不想看下去了。[/quote] 不用去写那个Alloc,不用管,直接用默认的Allocate就好了,你自己写搞不好出大问题。 shared_ptr<your_Date_Type >就行了。[/quote] 这不DD有点疼了嘛,就整一下玩玩。游戏中很多的基本上都是自己的分配器,缺省的分配器导致效率下降。[/quote] 那你去模仿其它的分配器自己写一个,应该问题不大。[/quote] 分配器倒是会弄,主要是确定下这个做法,以前也没去看标准。
漫步者、 2014-01-13
  • 打赏
  • 举报
回复
引用 4 楼 lx458004975 的回复:
[quote=引用 3 楼 ganpengjin1 的回复:] [quote=引用 2 楼 lx458004975 的回复:] [quote=引用 1 楼 ganpengjin1 的回复:] 恩,处理分配器,这个得小心使。
水区经常看到你哈。 分配器是有点麻烦,A shall be an allocator (17.6.3.5). 这一跳过去都是表,都不想看下去了。[/quote] 不用去写那个Alloc,不用管,直接用默认的Allocate就好了,你自己写搞不好出大问题。 shared_ptr<your_Date_Type >就行了。[/quote] 这不DD有点疼了嘛,就整一下玩玩。游戏中很多的基本上都是自己的分配器,缺省的分配器导致效率下降。[/quote] 那你去模仿其它的分配器自己写一个,应该问题不大。
偏爱风流 2014-01-13
  • 打赏
  • 举报
回复
引用 3 楼 ganpengjin1 的回复:
[quote=引用 2 楼 lx458004975 的回复:] [quote=引用 1 楼 ganpengjin1 的回复:] 恩,处理分配器,这个得小心使。
水区经常看到你哈。 分配器是有点麻烦,A shall be an allocator (17.6.3.5). 这一跳过去都是表,都不想看下去了。[/quote] 不用去写那个Alloc,不用管,直接用默认的Allocate就好了,你自己写搞不好出大问题。 shared_ptr<your_Date_Type >就行了。[/quote] 这不DD有点疼了嘛,就整一下玩玩。游戏中很多的基本上都是自己的分配器,缺省的分配器导致效率下降。
漫步者、 2014-01-13
  • 打赏
  • 举报
回复
引用 2 楼 lx458004975 的回复:
[quote=引用 1 楼 ganpengjin1 的回复:] 恩,处理分配器,这个得小心使。
水区经常看到你哈。 分配器是有点麻烦,A shall be an allocator (17.6.3.5). 这一跳过去都是表,都不想看下去了。[/quote] 不用去写那个Alloc,不用管,直接用默认的Allocate就好了,你自己写搞不好出大问题。 shared_ptr<your_Date_Type >就行了。
偏爱风流 2014-01-13
  • 打赏
  • 举报
回复
引用 1 楼 ganpengjin1 的回复:
恩,处理分配器,这个得小心使。
水区经常看到你哈。 分配器是有点麻烦,A shall be an allocator (17.6.3.5). 这一跳过去都是表,都不想看下去了。
漫步者、 2014-01-13
  • 打赏
  • 举报
回复
恩,处理分配器,这个得小心使。

64,639

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧