std:: make_unique, std:: make_unique_for_overwrite
|
헤더 파일에 정의됨
<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
로 래핑합니다.
T
를 생성합니다. 인수
args
는
T
의 생성자로 전달됩니다. 이 오버로드는
T
가 배열 타입이 아닌 경우에만 오버로드 해결에 참여합니다. 이 함수는 다음 코드와 동일합니다:
unique_ptr<T>(new T(std::forward<Args>(args)...))
T
가 경계를 알 수 없는 배열일 때만 오버로드 해결에 참여합니다. 이 함수는 다음 코드와 동일합니다:
unique_ptr<T>(new std::remove_extent_t<T>[size]())
T
가 배열 타입이 아닌 경우에만 오버로드 해결에 참여합니다. 이 함수는 다음 코드와 동등합니다:
unique_ptr<T>(new T)
T
가 경계를 알 수 없는 배열인 경우에만 오버로드 해결에 참여합니다. 이 함수는 다음 코드와 동등합니다:
unique_ptr<T>(new std::remove_extent_t<T>[size])
목차 |
매개변수
| args | - |
T
의 인스턴스가 생성될 인수 목록
|
| size | - | 생성할 배열의 길이 |
반환값
std::unique_ptr
타입
T
의 인스턴스.
예외
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
는 할당자 인식(allocator-aware) 대응 함수를 가지지 않습니다.
P0211
에서 제안된
allocate_unique
는 반환하는
std::
unique_ptr
<
T,D
>
에 대해 삭제자 타입
D
를 고안해야 하며, 이는 할당자 객체를 포함하고 해당
operator
(
)
에서
destroy
와
deallocate
를 모두 호출하게 됩니다.
| 기능 테스트 매크로 | 값 | 표준 | 기능 |
|---|---|---|---|
__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) |
constexpr
오버로드
(
1,2,4,5
)
|
예제
|
이 섹션은 불완전합니다
이유: 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
을 생성함
(공개 멤버 함수) |
|
|
새로운 객체를 관리하는 shared pointer를 생성함
(함수 템플릿) |