The following code example is taken from the book
C++23 - The Complete Guide
by Nicolai M. Josuttis,
Leanpub, 2026
The code is licensed under a
Creative Commons Attribution 4.0 International License.
// raw code
#include <print>
#include <vector>
#include <utility> // for std::move()
void debugColl(const std::vector<std::string>& coll, const std::string& name)
{
std::println("{}:", name);
for (const auto& s : coll) {
std::println(" [{}: {}]", static_cast<const void*>(s.data()), s);
}
}
void insertElemsInto(const std::vector<std::string>& coll, auto& dst)
{
for (const auto& elem : coll) {
dst.push_back(elem); // copy the element
}
}
void insertElemsInto(std::vector<std::string>&& coll, auto& dst)
{
for (auto&& elem : coll) {
dst.push_back(std::move(elem)); // move the element
}
}
int main()
{
std::vector<std::string> coll{"string1withSignificantSize",
"string2withSignificantSize"};
debugColl(coll, "coll");
std::vector<std::string> dest;
insertElemsInto(coll, dest); // copy elements of coll into dest
insertElemsInto(std::move(coll), dest); // move elements of coll into dest
debugColl(coll, "coll after copy and move");
debugColl(dest, "dest after copy and move");
}