find_if and mem_fun not getting expected results

J

jonnyothan

The following code isn't producing expected results:

class Selectable
{
bool IsSelected() const;
};

class Unit : public Selectable
{
};

typedef std::list<Unit *> UnitList;
UnitList * units = mapSurface->GetUnits();

UnitList::iterator selectedUnit = find_if(units->begin(), units-
end(), mem_fun(&Unit::IsSelected));

When no units are selected, find_if is still returning the first (and
only, in my test case) unit. I am even stepping through the calls to
mem_fun and IsSelected and it is returning false! What's even
stranger, the following code did the right thing:

UnitList::iterator selectedUnit = units->begin();
while(selectedUnit!=units->end())
{
if ((*selectedUnit)->IsSelected())
{
break;
}
++selectedUnit;
}

What am I doing wrong?!?

Thanks in advance!
 
J

jonnyothan

Turns out it was a compiler error. I'm not great at x86, but the
IsSelected function was only setting the low byte of eax to 0. Then
when it returned, eax was converted to 1 because the higher 3 bytes
had a non-zero value.

Changing IsSelected to return int fixed the problem.

This was using MSVC 7.1, by the way (I know, it's ancient).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top