shared_ptr别名构造函数 [英] shared_ptr aliasing constructor
问题描述
关于以下 shared_ptr
构造函数的问题:
类Y>
shared_ptr(const shared_ptr< Y& r,T * ptr);
如果 r
使用用户提供的deleter,然后别名 shared_ptr
知道。因此,如果别名 shared_ptr
是组中的最后一个,(当超出范围)破坏最初由 r
它将使用用户提供的删除程序?
示例:
#include< iostream>
#include< iomanip>
struct some_type
{
int i;
};
void my_deleter(some_type * p)
{
std :: cout< my_deleter called! << std :: endl;
delete p;
}
#include< memory>
int main()
{
std :: shared_ptr< int>下午;
{
//注意:更好地使用make_shared
auto x = new some_type;
//创建一个拥有x和一个删除者的shared_ptr
std :: shared_ptr< some_type> r(x,& my_deleter);
std :: cout<< r.use_count()<< std :: endl;
//共享x和删除者的所有权pm
pm = std :: shared_ptr< int>(r,& r-> i);
std :: cout<< r.use_count()<< std :: endl;
// r被销毁
}
std :: cout<< pm.use_count()<< std :: endl;
std :: cout<< get_deleter == 0?< std :: boolalpha
<< (nullptr == std :: get_deleter< decltype(& my_deleter)>(pm))
< std :: endl;
}
输出:
1
2
1
get_deleter == 0? false
my_deleter调用!
N.B。我无法使用自由函数 my_deleter
编译此示例,对于自由 get_deleter
函数有一些转换错误从 void *
转换为具有 static_cast
的函数指针类型。
别名ctor:
[util.smartptr.shared.const] / 13-14
< blockquote>
模板< class Y> shared_ptr(const shared_ptr< Y& r,T * p)noexcept;
13 效果:构造 14 后置条件: Ctor与用户提供的删除程序: 模板shared_ptr(Y * p,D d); 效果:构造拥有对象的 Dtor: 〜shared_ptr(); 1 效果: 结合这些(让我们跳过赋值运算符): Question about following Am I correct that if Example: Output: N.B. I can't compile this example with a free function Aliasing ctor:
[util.smartptr.shared.const]/13-14 shared_ptr
r
存储 p
和共享所有权的code> p>
get()== p&& use_count()== r.use_count()
[util.smartptr.shared.const] / 9
shared_ptr
对象 p
和删除程序 d
。
[util.smartptr.shared.dest] / 1
(
use_count()>)的共享所有权。 c / em>对象
p
和删除程序 d
, d(p)$ 指向
。
, delete p
。
shared_ptr c 。
shared_ptr
实例与 r
共享所有权
use_count> > > c>指向和删除程序(如果有),并且将使用此删除程序(如果存在)或
delete
shared_ptr
constructor:template< class Y >
shared_ptr( const shared_ptr<Y>& r, T *ptr );
r
was created using user-provided deleter, then aliasing shared_ptr
knows that. So if aliasing shared_ptr
is last in the group and (when going out of scope) destructs resources originally managed by r
, it will use that user-provided deleter?#include <iostream>
#include <iomanip>
struct some_type
{
int i;
};
void my_deleter(some_type* p)
{
std::cout << "my_deleter called!" << std::endl;
delete p;
}
#include <memory>
int main()
{
std::shared_ptr<int> pm;
{
// Note: better use make_shared
auto x = new some_type;
// create a shared_ptr that owns x and a deleter
std::shared_ptr<some_type> r(x, &my_deleter);
std::cout << r.use_count() << std::endl;
// share ownership of x and the deleter with pm
pm = std::shared_ptr<int>(r, &r->i);
std::cout << r.use_count() << std::endl;
// r gets destroyed
}
std::cout << pm.use_count() << std::endl;
std::cout << "get_deleter == 0? " << std::boolalpha
<< (nullptr == std::get_deleter<decltype(&my_deleter)>(pm))
<< std::endl;
}
1
2
1
get_deleter == 0? false
my_deleter called!
my_deleter
, there's some casting error for the free get_deleter
function (trying to cast from void*
to a function pointer type with a static_cast
).
template<class Y> shared_ptr(const shared_ptr<Y>& r, T *p) noexcept;
13 Effects: Constructs a shared_ptr
instance that stores p
and shares ownership with r
.
14 Postconditions: get() == p && use_count() == r.use_count()
Ctor with user-provided deleter: [util.smartptr.shared.const]/9
template shared_ptr(Y* p, D d);
Effects: Constructs a
shared_ptr
object that owns the objectp
and the deleterd
.
Dtor: [util.smartptr.shared.dest]/1
~shared_ptr();
1 Effects:
- If
*this
is empty or shares ownership with anothershared_ptr
instance (use_count() > 1
), there are no side effects.- Otherwise, if
*this
owns an objectp
and a deleterd
,d(p)
is called.- Otherwise,
*this
owns a pointerp
, anddelete p
is called.
Combining those (let's skip the assignment operators):
- The
shared_ptr
instancer
owns both the object and the deleter. - The aliasing ctor lets the new
shared_ptr
instance share ownership withr
(i.e. for both, the object and the deleter). - When the dtor of this new instance is called (or an assignment operator),
- If
use_count > 1
, no effects. - Else, this instance owns the object which
r
pointed to and the deleter (if any) and will either use this deleter (if it exists) ordelete
on the object pointed to.
- If
这篇关于shared_ptr别名构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!