C/C++ Arena

Step 6 of 7

Sink parameters and emplace_back

When a function is going to keep a string (store it in a member or a container), take it by value and move it into place. This is called a sink parameter:

explicit Player(std::string name) : name_(std::move(name)) {}

For containers, emplace_back(args...) constructs the element inside the vector from constructor arguments, instead of building a temporary and moving it in.

team.emplace_back("ropz");   // calls Player(std::string) in place

Your turn: give Player a sink constructor, and write void recruit(std::vector<Player>& team, std::string name) that adds a player using emplace_back and moves the name.

Previous: Moved-from objects Next: Challenge: a move-aware log