Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-09-02 16:37:23


AMDG

Zeljko Vrba wrote:
> Anyway, I've figured out how to define operator< for fusion::pairs, so I'm
> going down that route.

I would generally advise using a custom predicate rather than
adding to fusion's namespace.

struct compare_fusion_pair_second {
    template<typename T>
    bool operator()(bf::pair<T, unsigned> a, bf::pair<T, unsigned> b) const
    {
        return a.second < b.second;
    }
};

//...
std::sort(v.begin(), v.end(), compare_fusion_pair_second());

> But I'm curious about the anomalous std::sort behavior.
> Why doesn't sort actually sort the std::vector (search for line marked XXX:)?
> Should I watch out for more similar pitfalls?
>
>
> typedef bf::transform_view<Map, Map2Pair> Pair;
>
> Pair tx(Map m)
> {
> return Pair(m, Map2Pair());
> }
>

A transform_view stores a reference to the underlying sequence.
In this case, the underlying sequence immediately goes out of scope
and the transform_view is left with a dangling reference.
Try this.

Pair tx(Map& m)
{
    return Pair(m, Map2Pair());
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net