CS100 Tutorial 10 Fall 2025
1. 左/右值与拷贝/移动
参数转发
回顾 std::make_shared 和 std::make_unique:
auto sp = std::make_shared<std::string>(10, 'c');
auto sp2 = std::make_shared<std::string>("hello");
auto up = std::make_unique<Student>("Alice", "2020123123");
甚至,如果传入右值,它们会移动构造那个对象:
auto sp3 = std::make_shared<std::string>(std::move(*sp));
std::cout << *sp << std::endl;
这种将参数转发给另一个函数,又能保持它们的值类别的操作叫做完美转发 (perfect forwarding)