Re: STL map question : directed to stl map expert(s)....
On Sep 20, 12:21 am, James Dennett <jdenn...@acm.org> wrote:
Craig Scott wrote:
On Sep 20, 9:24 am, Alberto Ganesh Barbati <AlbertoBarb...@libero.it>
wrote:
(if you are wondering, the apparently identical code:
delete it->second;
myMap.erase(it);
invokes undefined behaviour, because deleting the pointer makes the
pointer uncopiable, thus you would be violating the container
pre-condition)
Actually, this second block of code (which you call apparently
identical code) is perfectly valid.
No, it is not (for the reason given originally).
Even if a deleted pointer value becomes "uncopyable", there is still
no requirement that the mapped_type of a std::map has to be copyable
in the first place. So the std::map precondition (that is supposedly
violated by the sample code) is not in fact being violated - because
no such precondition exists.
Whether those pointers actually
point to anything valid or not is completely irrelevant from the map's
perspective.
Here it falls down; an invalidated pointer not only does not
point to anything valid, it's also not CopyConstructible or
Assignable (in that any attempt to read its value gives UB).
There is nothing in the C++ Standard about the effects of copying,
assigning or reading a deallocated pointer value. The Standard states
only that the "use' of a deallocated pointer value has undefined
behavior - but never gets around to explaining what exactly it means
to "use" a value (see core language issue #312).
So whatever we may speculate about what it means to "use" a pointer
value, there can be no doubt that the line of code in question:
myMap.erase(it);
does not even retrieve the deleted pointer value from it->second -
much less "use" it in some way.
Greg
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]