Professional CMake:

A Practical Guide

Learn to use CMake effectively with practical advice from a CMake co-maintainer. You can also have the author work directly with your team!

Move constructors can copy and std::move doesn’t move anything

I recently came across an interesting use of std::move which looked something like the following:

void MyObject::processItems()
{
    std::vector<int> items(std::move(m_items));
    for (auto item : items)
    {
        // Do things which may add new items to m_items
    }
}

The intent of the code was that every time the member function processItems() was called, it would perform some operation on each item held in the member variable m_items. Each processed item should be removed from m_items. The operation to be performed might generate new items which would be added to m_items, so care had to be taken about how to iterate over the set of items.

To ensure robust iteration over the items to be processed, the code transfers the contents of m_items to a local object and then iterates over that local object. Thus, if new items are created during processing, they would be added to m_items and the container being iterated over (items) would not be affected. All good, right? Well, probably yes, but by no means is it guaranteed.

Read more