jeudi 29 décembre 2016

Does std::move() invalidate iterators?

Consider the following program:

struct list_wrapper
{
    std::vector<int>    m_list;
};

int main()
{
    std::vector<int> myList { 1, 1, 2, 3, 5 };

    const std::vector<int>::iterator iter = myList.begin();

    list_wrapper wrappedList;
    wrappedList.m_list = std::move(myList);

    // Can I still dereference iter?

    return 0;
}

After the call to std::move(myList), does iter now point to a valid item inside wrappedList.m_list, or do move constructors invalidate all iterators?

Aucun commentaire:

Enregistrer un commentaire