if ::iterator == 0 causes error:

Started by
4 comments, last by Qw3r7yU10p! 19 years, 8 months ago
I have a handle class. The handle class hold an iterator in to a map.

typename typedef std::map<std::string, std::pair<ResourceType*, size_t> >::iterator ResourceItr;

ResourceItr	m_Resource;
On construction my code sets the Resource handle to 0. Later I test for the validity of the handle :

template <class ResourceType, typename IOType>
inline bool CHandle<ResourceType, IOType>::IsNull(void) const
{
	return (m_Resource == 0); <<<<<<<<<<<<<<<<<<<<<
}
For some reason since upgrading to VS 2005 this test of wether a iterator is null is no longer valid. Can anyone think of why this may no longer be valid?
Chris Brodie
Advertisement
I don't think you're allowed to do that with iterators.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
don't test for null, test
if (it == container.end())
Assassin, aka RedBeard. andyc.org
hmmmm... I guess I could just add a bool to the hanlde class to track it that way. I can't use .end as the handle is hel by object that exist before the container exists and needs to be assigned later.

I'm quite suprised that I shouln't do that. I wonder where I picked that up from...
Chris Brodie
you want to test if the ResourceType* is NULL right?

well the map stores its data as a std::pair,
so to test for ResourceType*==0 you would have to do:

inline bool CHandle::IsNull(void) const
{
return (m_Resource->second.first == 0); <<<<
}

i have no idea what the overloaded == operator does in the map::iterator though... but it should never have worked...
If the iterator is actually just a pointer (which is quite possible) then it could evaluate correctly. I wonder whether the newer STL libraries have debug behaviour which detects misuse. Just a thought, no factual basis.

This topic is closed to new replies.

Advertisement