Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving objects from one unordered_map to another container

My question is that of safety. I've searched cplusplus.com and cppreference.com and they seem to be lacking on iterator safety during std::move. Specifically: is it safe to call std::unordered_map::erase(iterator) with an iterator whose object has been moved? Sample code:

#include <unordered_map>
#include <string>
#include <vector>
#include <iostream>
#include <memory>

class A {
public:
    A() : name("default ctored"), value(-1) {}
    A(const std::string& name, int value) : name(name), value(value) { }
    std::string name;
    int value;
};
typedef std::shared_ptr<const A> ConstAPtr;

int main(int argc, char **argv) {
    // containers keyed by shared_ptr are keyed by the raw pointer address
    std::unordered_map<ConstAPtr, int> valued_objects;

    for ( int i = 0; i < 10; ++i ) {
        // creates 5 objects named "name 0", and 5 named "name 1"
        std::string name("name ");
        name += std::to_string(i % 2);

        valued_objects[std::make_shared<A>(std::move(name), i)] = i * 5;
    }

    // Later somewhere else we need to transform the map to be keyed differently
    // while retaining the values for each object

    typedef std::pair<ConstAPtr, int> ObjValue;

    std::unordered_map<std::string, std::vector<ObjValue> > named_objects;

    std::cout << "moving..." << std::endl;

    // No increment since we're using .erase() and don't want to skip objects.
    for ( auto it = valued_objects.begin(); it != valued_objects.end(); ) {
        std::cout << it->first->name << "\t" << it->first.value << "\t" << it->second << std::endl;

        // Get named_vec.
        std::vector<ObjValue>& v = named_objects[it->first->name];
        // move object :: IS THIS SAFE??
        v.push_back(std::move(*it));

        // And then... is this also safe???
        it = valued_objects.erase(it);
    }

    std::cout << "checking... " << named_objects.size() << std::endl;
    for ( auto it = named_objects.begin(); it != named_objects.end(); ++it ) {
        std::cout << it->first << " (" << it->second.size() << ")" << std::endl;
        for ( auto pair : it->second ) {
            std::cout << "\t" << pair.first->name << "\t" << pair.first->value << "\t" << pair.second << std::endl;
        }
    }

    std::cout << "double check... " << valued_objects.size() << std::endl;
    for ( auto it : valued_objects ) {
        std::cout << it.first->name << " (" << it.second << ")" << std::endl;
    }

    return 0;
}

The reason I ask is that it strikes me that moving the pair from the unordered_map's iterator may (?) therefore *re*move the iterator's stored key value and therefore invalidate its hash; therefore any operations on it afterward could result in undefined behavior. Unless that's not so?

I do think it's worth noting that the above appears to successfully work as intended in GCC 4.8.2 so I'm looking to see if I missed documentation supporting or explicitly not supporting the behavior.

like image 962
inetknght Avatar asked Jun 09 '14 18:06

inetknght


1 Answers

// move object :: IS THIS SAFE??
v.push_back(std::move(*it));

Yes, it is safe, because this doesn't actually modify the key. It cannot, because the key is const. The type of *it is std::pair<const ConstAPtr, int>. When it is moved, the first member (the const ConstAPtr) is not actually moved. It is converted to an r-value by std::move, and becomes const ConstAPtr&&. But that doesn't match the move constructor, which expects a non-const ConstAPtr&&. So the copy constructor is called instead.

like image 132
Benjamin Lindley Avatar answered Oct 14 '22 08:10

Benjamin Lindley