このコンストラクタは非常に秘密であり、STLメンテナでさえそれを知らない...ステファン・T・ラヴァヴェージ
このコンストラクターは非常に秘密であるため、付随するSTLでさえもそのことを知りません...あたり: ドア
std::shared_ptr
には少し秘密があります:ほとんどのプログラマーが聞いたこともない非常に便利なコンストラクターです。 これは、C ++ 11標準でのみ追加され、
shared_ptr
TR1バージョンでもありませんでした。 ただし、バージョン4.3以降のgccおよびVisual Studio 2010以降のMSVCコンパイラーでサポートされています。ブーストには約1.35.0から登場しました。
std::shared_ptr
を説明するほとんどのチュートリアルには、このコンストラクターに関する情報はありません。 スコットマイヤーズは、Effective Modern C ++で彼について一言も述べませんでした。別の著者であるニコライジョシュティスは、彼の著書The C ++ Standard Libraryでこのコンストラクターの約半分をページに捧げました。
それで、この秘密のコンストラクタは何ですか?
これはエイリアスコンストラクタです。
エイリアスshared_ptr
このコンストラクタは何をしますか? これにより、所有権を別の
shared_ptr
と共有する
shared_ptr
を作成できますが、(注意!)異なるポインターがあります。 そして、あるタイプから別のタイプに単純にキャストされた別のタイプのポインターを意味するのではなく、ポインターの完全に異なる値について話している。 つまり、
shared_ptr<std::string>
と
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
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