#include <iostream>
#include <memory>
#include <string>
#include <utility>
struct User {
std::string name;
int age;
User(std::string n, int a) : name(std::move(n)), age(a) {}
};
// *** before: ***
// C++11 brought make_shared but forgot make_unique, so new was still
// written by hand and the type was named twice
std::unique_ptr<User> createOld() {
return std::unique_ptr<User>(new User("Ann", 31));
}
// *** in version C++14: ***
std::unique_ptr<User> create() {
return std::make_unique<User>("Bob", 27); // the type is named once
}
int main() {
auto a = createOld();
auto b = create();
auto many = std::make_unique<int[]>(4); // an array works too
many[0] = 5;
std::cout << a->name << " " << b->name << " " << many[0] << "\n";
}