This line testmap.insert(pair<int, ABC> (0, a)); can generate 3 ABC copy constructors
as follows:
1. the variable a is copied to pass to the pair constructor.
2. The pair object created is step 1 above is then copied to pass map.insert function.
(This means that the ABC copy constructor is called on the ABC object contained in the pair)
3. The insert function copies the pair object passed to it.
(hence the pair object is copied which means that the ABC copy constructor is called againis copied again)
So there are 3 copy constructor calls.
EDIT:
In the case of MSVC2010 and the 2 copy constructors - VC2010 is using the movesemantics from the new C++0x standard which allows it to eliminate 1 copy constructor call ( one less call to the pair copy constructor, which means one less call to the ABC copy constructor).
**I still don't understand this new move semantisc stuff yet though**
The STL containers are known for doing copying. If I remember right, to use them, your class must be default constructable and copyable. I don't think this varies by compiler, but rather varies by standard library implementation.
AFAIK the STL doesn't ask for defaults constructors.
Well one exception is operator[] for maps, that calls insert( make_pair( key, T() ) )
But you have the same functionallity with find and insert
2. The pair object created is step 1 above is then copied to pass map.insert function.
(This means that the ABC copy constructor is called on the ABC object contained in the pair)
3. The insert function copies the pair object passed to it.
(hence the pair object is copied which means that the ABC copy constructor is called againis copied again)
Your 2nd and 3rd step are the same. I got the answer from another place. The third copy is created in the insert itself. The map is implemented as a binary tree. In order to add the pair to the tree, the pair is copied again, which calls the copy ctor for the third time.
If you pass a reference or a pointer to the object of ABC, all the three copy ctor calls can be eliminated.
Your 2nd and 3rd step are the same. I got the answer from another place. The third copy is created in the insert itself. The map is implemented as a binary tree. In order to add the pair to the tree, the pair is copied again, which calls the copy ctor for the third time.
no they aren't - you either miread or misunderstood what I was saying.