Boost logo

Boost :

From: Gennaro Prota (gennaro_prota_at_[hidden])
Date: 2003-03-28 12:50:19


On Fri, 28 Mar 2003 12:28:56 -0500, Jan Langer <jan_at_[hidden]>
wrote:

>Gennaro Prota wrote:
>> The point is that your operator bool is a loss
>> of information. Ever heard of lexicographical_compare_3way?
>
>yes, but never used so far. but the non_3way lexicographic compare
>algorithm has the same problem.

Indeed.

>[...]
>> Hoped to make some more intelligent point, but if we don't understand
>> each other on such basics first... :-(
>
>i dont really know what you mean.

Ok. This is my last post in this thread. Let me just add that your
conversion function to bool, together with the default constructor you
provide, lets the user take the "result of the comparison" without
actually doing any call to operator().

I would rather see an approach which forces a call before enabling any
implicit conversion. Just to illustrate the idea (don't expect this to
compile, I'll just type it in the news-reader window):

class compare
{
public:
    enum result { minus, equiv, plus };

private:
    class R {
        friend class compare;

        result v_;

        R():v_(equiv) {} // it's not really "equiv", because
                         // no elements have been compared,
                         // but the user can't see this

        public:

        template <typename T>
        R & operator()(const T& a, const T& b) {
            if (v_ == equiv) {
                if (a < b)
                    v_ = minus;
                else if (b < a)
                    v_ = plus;
            }
            return *this;
        }

        template <typename T, typename Cmp>
        R& operator()(const T&a, const T&b, Cmp cmp) {
            if (v_ == equiv) {
                if ( cmp(a, b) )
                    v_ = minus;
                else if ( cmp(b, a) )
                    v_ = plus;
            }
            return *this;
        }
        operator result() const { return v_; }

    };

    mutable R result_;

public:

        compare ():result_() {}

        template <typename T>
        R &operator () (const T& a, const T& b) const
        {
            return result_(a, b);
        }

        template <typename T, typename Cmp>
        R& operator()(const T& a, const T& b, Cmp cmp ) const
        {
            return result_(a, b, cmp);
        }

};

Genny.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk