Interesting Boost::bind error?

Discussion in 'C++' started by joe, Mar 10, 2008.

  1. joe

    joe Guest

    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?
     
    joe, Mar 10, 2008
    #1
    1. Advertising

  2. joe

    James Kanze Guest

    On Mar 10, 9:58 pm, joe <> wrote:
    > 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.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Mar 11, 2008
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Toby Bradshaw
    Replies:
    6
    Views:
    1,754
    Kai-Uwe Bux
    Jun 2, 2006
  2. Replies:
    4
    Views:
    424
  3. Replies:
    0
    Views:
    591
  4. Christopher
    Replies:
    1
    Views:
    812
    Yakov Gerlovin
    Oct 5, 2011
  5. Mark
    Replies:
    1
    Views:
    434
    Jeff Flinn
    Nov 25, 2012
Loading...

Share This Page