std::make_shared, std::make_shared_for_overwrite
cppreference.com
<tbody>
</tbody>
template< class T, class... Args > shared_ptr<T> make_shared( Args&&... args ); |
(1) | ( C++11) (T ) |
template< class T > shared_ptr<T> make_shared( std::size_t N ); |
(2) | ( C++20) (T U[]) |
template< class T > shared_ptr<T> make_shared(); |
(3) | ( C++20) (T U[N]) |
template< class T > shared_ptr<T> make_shared( std::size_t N, const std::remove_extent_t<T>& u ); |
(4) | ( C++20) (T U[]) |
template< class T > shared_ptr<T> make_shared( const std::remove_extent_t<T>& u ); |
(5) | ( C++20) (T U[N]) |
template< class T > shared_ptr<T> make_shared_for_overwrite(); |
(6) | ( C++20) (T U[]) |
template< class T > shared_ptr<T> make_shared_for_overwrite( std::size_t N ); |
(7) | ( C++20) (T U[]) |
1)
T std::shared_ptr, args T. ::new (pv) T(std::forward<Args>(args)...), pv void* , T. , sizeof(T), , T. std::shared_ptr, , shared_from_this T. |
, T |
( C++20) |
2,3) , (1), , , , , ,
std::remove_all_extents_t<T> , new ::new(pv) std::remove_all_extents_t<T>(). (2) N . , , , .4,5) , (2,3),
u. U , new, (1); , (, ), , u new, (1). (4) N . , , , . ( , T ) ( C++20) p->~X(), p , X .
| args | , T.
| |
| N | ||
| u |
std::bad_alloc , T. , . , . ( C++20)
std::shared_ptr<T>(new T(args...)). :
std::shared_ptr<T>(new T(args...))(T),std::make_shared<T>( , ; )- - std::weak_ptr ,
std::make_shared, ,T, , , ,sizeof(T). std::shared_ptr<T>(new T(args...))T, , ,std::make_shared.- std::shared_ptr,
std::make_shared. std::make_shared::new, , - operator new,std::shared_ptr<T>(new T(args...)).
|
( C++20) |
| ( C++17) |
shared_from_this ptr U* , , U ( C++17) , std::enable_shared_from_this, , :
if (ptr != nullptr && ptr->weak_this.expired())
ptr->weak_this = std::shared_ptr<std::remove_cv_t<U>>(
*this, const_cast<std::remove_cv_t<U>*>(ptr));
weak_this mutable std::weak_ptr std::enable_shared_from_this. weak_this . , shared_from_this() std::shared_ptr, .
ptr->weak_this.expired() , weak_this , . C++17.
__cpp_lib_shared_ptr_arrays |
201707L |
(C++20) | std::make_shared; (2-5)
|
__cpp_lib_smart_ptr_for_overwrite |
202002L |
(C++20) | (std::allocate_shared_for_overwrite, std::make_shared_for_overwrite, std::make_unique_for_overwrite); (6,7)
|
#include <memory>
#include <vector>
#include <iostream>
#include <type_traits>
struct C
{
// ( C++20)
C(int i) : i(i) {}
C(int i, float f) : i(i), f(f) {}
int i;
float f{};
};
int main()
{
// `auto` `sp1`
auto sp1 = std::make_shared<C>(1); // (1)
static_assert(std::is_same_v<decltype(sp1), std::shared_ptr<C>>);
std::cout << "sp1->{ i:" << sp1->i << ", f:" << sp1->f << " }\n";
// `sp2`
std::shared_ptr<C> sp2 = std::make_shared<C>(2, 3.0f); // (1)
static_assert(std::is_same_v<decltype(sp2), std::shared_ptr<C>>);
static_assert(std::is_same_v<decltype(sp1), decltype(sp2)>);
std::cout << "sp2->{ i:" << sp2->i << ", f:" << sp2->f << " }\n";
// shared_ptr float[64]; (2):
std::shared_ptr<float[]> sp3 = std::make_shared<float[]>(64);
// shared_ptr long[5][3][4]; (2):
std::shared_ptr<long[][3][4]> sp4 = std::make_shared<long[][3][4]>(5);
// shared_ptr short[128]; (3):
std::shared_ptr<short[128]> sp5 = std::make_shared<short[128]>();
// shared_ptr int[7][6][5]; (3):
std::shared_ptr<int[7][6][5]> sp6 = std::make_shared<int[7][6][5]>();
// shared_ptr double[256], 2.0; (4):
std::shared_ptr<double[]> sp7 = std::make_shared<double[]>(256, 2.0);
// shared_ptr double[7][2], double[2]
// {3.0, 4.0}; (4):
std::shared_ptr<double[][2]> sp8 = std::make_shared<double[][2]>(7, {3.0, 4.0});
// shared_ptr vector<int>[4],
// {5, 6}; (4):
std::shared_ptr<std::vector<int>[]> sp9 =
std::make_shared<std::vector<int>[]>(4, {5, 6});
// shared_ptr float[512], 1.0; (5):
std::shared_ptr<float[512]> spA = std::make_shared<float[512]>(1.0);
// shared_ptr double[6][2], double[2]
// {1.0, 2.0}; (5):
std::shared_ptr<double[6][2]> spB = std::make_shared<double[6][2]>({1.0, 2.0});
// shared_ptr vector<int>[4],
// {5, 6}; (5):
std::shared_ptr<std::vector<int>[4]> spC =
std::make_shared<std::vector<int>[4]>({5, 6});
}
:
sp1->{ i:1, f:0 }
sp2->{ i:2, f:3 }
shared_ptr (public -) | |
| , , ( ) | |
(C++11) |
shared_ptr, ( ) |
(C++14)(C++20) |
, ( ) |
| () |