Interesting Boost::bind error?

J

joe

I expected I could construct code like the following:

return *(std::min_element(vec.begin(), vec.end(),
bind(less<double>(),
bind(&entry::foo, _1),
bind(&entry::foo, _2))));

But if vec is only size 1, this returns '0'. min_element should work
on size one vectors.
If I add the guard "if(vec.size() == 1) { return *(vec.begin()); } "

all works fine..

Looking at the implementation code for min_element, this predicate
shouldn't even be called. Any ideas? Is boost messing with this
somehow?
 
J

James Kanze

I expected I could construct code like the following:
return *(std::min_element(vec.begin(), vec.end(),
bind(less<double>(),
bind(&entry::foo, _1),
bind(&entry::foo, _2))));
But if vec is only size 1, this returns '0'. min_element should work
on size one vectors.

And for zero size elements, but you'd better not dereference the
return value of min_element if the vector is empty.
If I add the guard "if(vec.size() == 1) { return *(vec.begin()); } "
all works fine..
Looking at the implementation code for min_element, this
predicate shouldn't even be called. Any ideas? Is boost
messing with this somehow?

You don't give enough information to be able to judge.

According to the standard, the predicate will be called at most
vec.size() - 1 times. So unless there's an error in the
implementation of min_element (which would surprise me greatly),
you've probably got undefined behavior elsewhere in your code.

I gave it a quick test, with what I presumed to be a simplified
definition of Entry, and it worked perfectly for me---using g++
4.1.0 and Boost 1.33. I doubt the problem is Boost, and unless
you've stumbled on a really poor implementation of the STL, I
doubt that it's there either. And it's not in the line you
posted, at least if you've asserted !vec.empty() before.
 

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top