-1

I am trying to practice how to move one map element to other - hence I tried below code:

using namespace std;

int main(void)
{
    /* Initializer_list constructor */
    map<char, int> m =
    {
        {'a', 1},
        {'b', 2},
        {'c', 3},
        {'d', 4},
        {'e', 5},
    };

    cout << "Move element from one map to another" << endl;
    /*
    char temp;
    temp = map1[key1];
    map2[key1]=temp;
    map1.erase(key1)
    */
    string a = "hello";
    string b = move(a);
    cout << "a=" << a << " b=" << b << endl; // here string **a** is NULL as value is moved
    auto s = move(m['a']);
    cout << "s=" << s << " m=" << m['a'] <<  endl; // here 
} 

Output :

Move element from one map to another                                                                                        
a= b=hello                                                                                                              
s=1 m=1                    

Why move operation is failing for std::map STL container - I was expecting that after m['a'] would be empty?

Sercan
  • 2,081
  • 2
  • 10
  • 23
Programmer
  • 8,303
  • 23
  • 78
  • 162
  • Does this answer your question? [What lasts after using std::move c++11](https://stackoverflow.com/questions/20850196/what-lasts-after-using-stdmove-c11) –  Jun 20 '20 at 04:37

2 Answers2

1

Nowhere does your code remove an object from the map. That would require an operation on the map, not an object in it. No operation on an object in a map would remove that object from the map.

The operation you perform on the object in the map is that you move its value from it. The state of an object after it has been moved from is legal but indeterminate.

So the object in the map is in a valid but indeterminate state.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
  • But we also did not do after move to the string object - why we explicitly need to erase or delete from map? – Programmer Jun 20 '20 at 04:36
  • @Programmer If you want to remove an object from a map, you have to perform some kind of "remove an object" operation on the map. No operation on the object will remove it from the map since the object doesn't know anything about maps. – David Schwartz Jun 20 '20 at 04:37
  • OK so what if I do : auto s = move(m); - is this the right way to move map into another map variable? – Programmer Jun 20 '20 at 04:50
  • @Programmer If you're trying to remove an object from a map, you have to perform some kind of "remove object" operation on the map. Moving the object's value out of the object won't remove the object from the map. Think of a move as just an optimized version of `x = y;` for cases where you don't need the value to also be in `y`. – David Schwartz Jun 20 '20 at 06:36
0

Why move operation is failing for std::map STL container - I was expecting that after m['a'] would be empty?

That expectation is ill-founded.

The statement

auto s = move(m['a']);

leaves m['a'] as is for couple of reasons.

  1. That statement can be thought of as:

    int&& s = move(m['a']);
    

    I.e it just captures reference to the object in the map. It does not actually move it.

  2. A move operation on an int doesn't alter the object being moved. Move semantics are significant only by move constructors of classes. For built-in types, it's a noop. Even for classes that don't actually acquire any resource beyond the member variables, there isn't much a move constructor can do.

Also, There is no such thing as an empty int. The notion of empty-ness makes sense only for containers.

R Sahu
  • 204,454
  • 14
  • 159
  • 270