This constructor is so secret, not even STL maintainers know about it ...Stephan T. Lavavej
This constructor is so secret that even accompanying STL do not know about it ...lane: Door
std::shared_ptr
has a small secret: a very useful constructor that most programmers haven’t even heard about. It was added only in standard C ++ 11, and it was not even in the TR1 version of shared_ptr
. However, it is supported by gcc from version 4.3, and by the MSVC compiler from the time of Visual Studio 2010. It appeared in Boost from about 1.35.0.std::shared_ptr
have nothing about this constructor. Scott Myers did not say a word about him in Effective Modern C ++, another author Nicolai Josuttis devoted to this designer about half a page in his book The C ++ Standard Library.shared_ptr
, which shares ownership with another shared_ptr
, but (attention!) Has a different pointer. And I do not mean a pointer of another type, which was simply cast from one type to another, I am talking about a completely different value of the pointer. That is, you can have both shared_ptr<std::string>
and shared_ptr, ( - ).
, , shared_ptr' . , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
at the same time shared_ptr, ( - ).
, , shared_ptr' . , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
shared_ptr, ( - ).
, , shared_ptr'
. , , , .
, ? shared_ptr
, ( , ) .
, X, Y,
struct X{ Y y; };
, shared_ptr, px
px->y
, shared_ptr . : shared_ptr, , , , Y. , , shared_ptr , px
?
struct do_nothing_deleter{ template<typename> void operator()(T*){} }; void store_for_later(std::shared_ptr<Y>); void foo(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(&px->y,do_nothing_deleter()); store_for_later(py); } // X
shared_ptr , , , . , . , , , shared_ptr shared_ptr. shared_ptr .
void bar(){ std::shared_ptr<X> px(std::make_shared<X>()); std::shared_ptr<Y> py(px,&px->y); store_for_later(py); } // X
. - , shared_ptr
. X2, Y,
struct X2{ std::unique_ptr<Y> y; X2():y(new Y){} }; void baz(){ std::shared_ptr<X2> px(std::make_shared<X2>()); std::shared_ptr<Y> py(px,px->y.get()); store_for_later(py); } // X2
, pimpl
. , , , . . X , shared_ptr, shared_ptr , shared_ptr (.. reset()
).
:
template<typename Other,typename Target> shared_ptr(shared_ptr<Other> const& other,Target* p);
shared_ptr p
*
, . Other
. other
, other.use_count()
1. , get()
- static_cast<T*>(p)
.
: other
shared_ptr
, , , shared_ptr
, use_count() == 0
. , p
NULL.
int i; shared_ptr<int> sp(shared_ptr<X>(),&i); assert(sp.use_count()==0); assert(sp.get()==&i);
.
shared_ptr
, .
, , .
reddit
cppreference
Source: https://habr.com/ru/post/263751/
All Articles