C++14에 unique_ptr를 생성하는 함수(creation function)로 make_unique()가 도입되었다.
동시에 여러 개의 unique_ptr을 생성하는 방법도 가능하다.
#include <iostream>
#include <memory>
#include <string>
using namespace std;
int main(void)
{
unique_ptr<int> int_ptr1 = make_unique<int>();
cout << *int_ptr1 << endl;
unique_ptr<int> int_ptr2 = make_unique<int>(34);
cout << *int_ptr2 << endl;
unique_ptr<string> str_ptr1 = make_unique<string>();
cout << '\"' << *str_ptr1 << '\"' << endl;
unique_ptr<string> str_ptr2 = make_unique<string>("hello");
cout << '\"' << *str_ptr2 << '\"' << endl;
auto up = make_unique<int[]>(5);
for (int i = 0; i < 5; ++i) {
cout << up[i] << endl;
}
return 0;
}
0
34
""
"hello"
0
0
0
0
0