-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprefer_make_unique_to_new.cpp
More file actions
27 lines (25 loc) · 975 Bytes
/
prefer_make_unique_to_new.cpp
File metadata and controls
27 lines (25 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <memory>
#include <iostream>
#include <vector>
class Widget {};
void ProcessWidget(std::shared_ptr<Widget> /*spw*/, int /*priority*/) {}
int ComputePriority() { return 1; }
int main() {
auto upw(std::make_unique<Widget>());
std::unique_ptr<Widget> upw2(new Widget);// code duplication for 'Widget'
ProcessWidget(std::shared_ptr<Widget>(new Widget),
ComputePriority());// potential resource leak!
ProcessWidget(std::make_shared<Widget>(),
ComputePriority());// no potential resource leak!
auto upv = std::make_unique<std::vector<int>>(10, 42);
std::cout << upv->at(0) << '\n';
auto initlist = {10, 42};
auto upv2 = std::make_unique<std::vector<int>>(initlist);
std::cout << upv2->at(0) << '\n';
auto CustomDeleter = [](Widget *p) {
delete p;
};
std::shared_ptr<Widget> spw(new Widget, CustomDeleter);
ProcessWidget(std::move(spw), ComputePriority());// both efficient and exception safe
return 0;
}