std::make_unique, std::make_unique_for_overwrite
From cppreference.com
< cpp | memory | unique ptr
| ヘッダ <memory> で定義 |
||
| (1) | ||
template< class T, class... Args > unique_ptr<T> make_unique( Args&&... args ); |
(C++14以降) (C++23まで) (非配列型のみ) |
|
| template< class T, class... Args > constexpr unique_ptr<T> make_unique( Args&&... args ); |
(C++23から) (非配列型のみ) |
|
| (2) | ||
template< class T > unique_ptr<T> make_unique( std::size_t size ); |
(C++14以降) (C++23まで) (要素数が不明な配列型のみ) |
|
| template< class T > constexpr unique_ptr<T> make_unique( std::size_t size ); |
(C++23から) (要素数が不明な配列型のみ) |
|
| template< class T, class... Args > /* 未指定 */ make_unique( Args&&... args ) = delete; |
(3) | (C++14以降) (要素数が既知の配列型のみ) |
| (4) | ||
template< class T > unique_ptr<T> make_unique_for_overwrite(); |
(C++20以降) (C++23まで) (非配列型のみ) |
|
| template< class T > constexpr unique_ptr<T> make_unique_for_overwrite(); |
(C++23から) (非配列型のみ) |
|
| (5) | ||
template< class T > unique_ptr<T> make_unique_for_overwrite( std::size_t size ); |
(C++20以降) (C++23まで) (要素数が不明な配列型のみ) |
|
| template< class T > constexpr unique_ptr<T> make_unique_for_overwrite( std::size_t size ); |
(C++23から) (要素数が不明な配列型のみ) |
|
| template< class T, class... Args > /* 未指定 */ make_unique_for_overwrite( Args&&... args ) = delete; |
(6) | (C++20以降) (要素数が既知の配列型のみ) |
型`T`のオブジェクトを構築し、それを std::unique_ptr でラップする。
1) 非配列型 `T` を構築する。引数 args は `T` のコンストラクタに渡される。このオーバーロードは、`T` が配列型でない場合にのみオーバーロード解決に参加する。この関数は以下のものと等価である。
unique_ptr<T>(new T(std::forward<Args>(args)...))
2) 指定された動的サイズの配列を構築する。配列要素は値初期化される。このオーバーロードは、`T` が要素数不明の配列である場合にのみオーバーロード解決に参加する。この関数は以下のものと等価である。
unique_ptr<T>(new std::remove_extent_t<T>[size]())
3,6) 要素数が既知の配列の構築は許可されない。
4) (1) と同じだが、オブジェクトがデフォルト初期化される点が異なる。このオーバーロードは、`T` が配列型でない場合にのみオーバーロード解決に参加する。この関数は以下のものと等価である。
unique_ptr<T>(new T)
5) (2) と同じだが、配列がデフォルト初期化される点が異なる。このオーバーロードは、`T` が要素数不明の配列である場合にのみオーバーロード解決に参加する。この関数は以下のものと等価である。
unique_ptr<T>(new std::remove_extent_t<T>[size])
目次 |
[編集] パラメータ
| args | - | `T` のインスタンスを構築するために使用される引数のリスト |
| size | - | 構築する配列の長さ |
[編集] 戻り値
型 `T` のインスタンスの std::unique_ptr。
[編集] 例外
std::bad_alloc または `T` のコンストラクタによってスローされる任意の例外をスローする可能性がある。例外がスローされた場合、この関数は効果がない。
[編集] 実装例
| make_unique (1-3) |
|---|
// C++14 make_unique namespace detail { template<class> constexpr bool is_unbounded_array_v = false; template<class T> constexpr bool is_unbounded_array_v<T[]> = true; template<class> constexpr bool is_bounded_array_v = false; template<class T, std::size_t N> constexpr bool is_bounded_array_v<T[N]> = true; } // namespace detail template<class T, class... Args> std::enable_if_t<!std::is_array<T>::value, std::unique_ptr<T>> make_unique(Args&&... args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)...)); } template<class T> std::enable_if_t<detail::is_unbounded_array_v<T>, std::unique_ptr<T>> make_unique(std::size_t n) { return std::unique_ptr<T>(new std::remove_extent_t<T>[n]()); } template<class T, class... Args> std::enable_if_t<detail::is_bounded_array_v<T>> make_unique(Args&&...) = delete; |
| make_unique_for_overwrite (4-6) |
// C++20 make_unique_for_overwrite template<class T> requires (!std::is_array_v<T>) std::unique_ptr<T> make_unique_for_overwrite() { return std::unique_ptr<T>(new T); } template<class T> requires std::is_unbounded_array_v<T> std::unique_ptr<T> make_unique_for_overwrite(std::size_t n) { return std::unique_ptr<T>(new std::remove_extent_t<T>[n]); } template<class T, class... Args> requires std::is_bounded_array_v<T> void make_unique_for_overwrite(Args&&...) = delete; |
[編集] 備考
std::make_shared にはstd::allocate_sharedがあるが、`std::make_unique`にはアロケータ対応の同等機能がない。P0211で提案された`allocate_unique`は、返されるstd::unique_ptr<T,D>のために、アロケータオブジェクトを含み、operator()内で`destroy`と`deallocate`の両方を呼び出すデリータ型`D`を発明する必要があるだろう。
| 機能テストマクロ | 値 | 規格 | 機能 |
|---|---|---|---|
__cpp_lib_make_unique |
201304L |
(C++14) | std::make_unique; オーバーロード (1) |
__cpp_lib_smart_ptr_for_overwrite |
202002L |
(C++20) | デフォルト初期化によるスマートポインタの作成 (std::allocate_shared_for_overwrite, std::make_shared_for_overwrite, std::make_unique_for_overwrite); オーバーロード (4-6) |
__cpp_lib_constexpr_memory |
202202L |
(C++23) | オーバーロード (1,2,4,5) のための constexpr |
[編集] 例
| このセクションは未完成です 理由: make_unique_for_overwrite() のデモを追加 |
このコードを実行
#include <cstddef> #include <iomanip> #include <iostream> #include <memory> #include <utility> struct Vec3 { int x, y, z; // Following constructor is no longer needed since C++20. Vec3(int x = 0, int y = 0, int z = 0) noexcept : x(x), y(y), z(z) {} friend std::ostream& operator<<(std::ostream& os, const Vec3& v) { return os << "{ x=" << v.x << ", y=" << v.y << ", z=" << v.z << " }"; } }; // Output Fibonacci numbers to an output iterator. template<typename OutputIt> OutputIt fibonacci(OutputIt first, OutputIt last) { for (int a = 0, b = 1; first != last; ++first) { *first = b; b += std::exchange(a, b); } return first; } int main() { // Use the default constructor. std::unique_ptr<Vec3> v1 = std::make_unique<Vec3>(); // Use the constructor that matches these arguments. std::unique_ptr<Vec3> v2 = std::make_unique<Vec3>(0, 1, 2); // Create a unique_ptr to an array of 5 elements. std::unique_ptr<Vec3[]> v3 = std::make_unique<Vec3[]>(5); // Create a unique_ptr to an uninitialized array of 10 integers, // then populate it with Fibonacci numbers. std::unique_ptr<int[]> i1 = std::make_unique_for_overwrite<int[]>(10); fibonacci(i1.get(), i1.get() + 10); std::cout << "make_unique<Vec3>(): " << *v1 << '\n' << "make_unique<Vec3>(0,1,2): " << *v2 << '\n' << "make_unique<Vec3[]>(5): "; for (std::size_t i = 0; i < 5; ++i) std::cout << std::setw(i ? 30 : 0) << v3[i] << '\n'; std::cout << '\n'; std::cout << "make_unique_for_overwrite<int[]>(10), fibonacci(...): [" << i1[0]; for (std::size_t i = 1; i < 10; ++i) std::cout << ", " << i1[i]; std::cout << "]\n"; }
出力
make_unique<Vec3>(): { x=0, y=0, z=0 }
make_unique<Vec3>(0,1,2): { x=0, y=1, z=2 }
make_unique<Vec3[]>(5): { x=0, y=0, z=0 }
{ x=0, y=0, z=0 }
{ x=0, y=0, z=0 }
{ x=0, y=0, z=0 }
{ x=0, y=0, z=0 }
make_unique_for_overwrite<int[]>(10), fibonacci(...): [1, 1, 2, 3, 5, 8, 13, 21, 34, 55][編集] 関連項目
新しい unique_ptr を構築する(public member function) | |
| 新しいオブジェクトを管理する共有ポインタを作成します (関数テンプレート) |