Topic: compare function constructors


Author: ersmith@ucsd.edu (Erik Smith)
Date: 1997/04/23
Raw View

I often need to use compare functions that reference objects outside of
the class of objects being compared.  I have found that I can define a
comparison function as follows:

template <class T>
struct Compare1 : binary_function<T, T, bool> {
    typedef typename binary_function<T, T, bool>::first_argument_type
                     first_argument_type;
    typedef typename binary_function<T, T, bool>::second_argument_type
                     second_argument_type;
    typedef typename binary_function<T, T, bool>::result_type
                     result_type;

    Compare1(int n):num(n) {
    }
    int num;

    bool operator () (const T& a, const T& b) const {
      // comparison code
   }
};

and then use it like this:

     j = lower_bound(a.begin(),a.end(),k,Compare1<A>(2));

but this doesn't work in a constructor context:

     multiset<A,Compare1<A>(2)> a;

Any ideas on how to get this to work?


erik
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Oleg Zabluda <zabluda@math.psu.edu>
Date: 1997/04/23
Raw View
Erik Smith <ersmith@ucsd.edu> wrote:
: template <class T>
: struct Compare1 : binary_function<T, T, bool> {
:     typedef typename binary_function<T, T, bool>::first_argument_type
:                      first_argument_type;
:     typedef typename binary_function<T, T, bool>::second_argument_type
:                      second_argument_type;
:     typedef typename binary_function<T, T, bool>::result_type
:                      result_type;

:     Compare1(int n):num(n) {
:     }
:     int num;

:     bool operator () (const T& a, const T& b) const {
:       // comparison code
:    }
: };

: and then use it like this:

:      j = lower_bound(a.begin(),a.end(),k,Compare1<A>(2));

: but this doesn't work in a constructor context:

:      multiset<A,Compare1<A>(2)> a;

: Any ideas on how to get this to work?


How about making ``2'' one of template arguments:
template <class T, int num>
struct Compare1 {
// ....
};

multiset<A,Compare1<A, 2> > a;

Oleg.
--
Life is a sexually transmitted, 100% lethal disease.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: James Kanze <james-albert.kanze@vx.cit.alcatel.fr>
Date: 1997/04/25
Raw View
ersmith@ucsd.edu (Erik Smith) writes:

|>  I often need to use compare functions that reference objects outside of
|>  the class of objects being compared.  I have found that I can define a
|>  comparison function as follows:
|>
|>  template <class T>
|>  struct Compare1 : binary_function<T, T, bool> {
|>      typedef typename binary_function<T, T, bool>::first_argument_type
|>                       first_argument_type;
|>      typedef typename binary_function<T, T, bool>::second_argument_type
|>                       second_argument_type;
|>      typedef typename binary_function<T, T, bool>::result_type
|>                       result_type;
|>
|>      Compare1(int n):num(n) {
|>      }
|>      int num;
|>
|>      bool operator () (const T& a, const T& b) const {
|>        // comparison code
|>     }
|>  };
|>
|>  and then use it like this:
|>
|>       j = lower_bound(a.begin(),a.end(),k,Compare1<A>(2));
|>
|>  but this doesn't work in a constructor context:
|>
|>       multiset<A,Compare1<A>(2)> a;
|>
|>  Any ideas on how to get this to work?

First, the reason you are having the problem is simple: the function
takes an object, whereas the declaration instantiates a template over a
type.  "Compare1< A >" is a type, and the expression "Compare1< A >( 2 )"
is a temporary object of that type.  The declaration of the variable
must then be:

 multiset< A , Compare1< A > >
      a ;

This won't work, howver, because in fact, the "default" constructor of a
multiset in fact requires an argument of the comparison type; the
default value is to use the default constructor, and your Compare1
doesn't have a default constructor.  So you must write:

 multiset< A , Compare1< A > >
      a( Compare1< A >( 2 ) ) ;


--
James Kanze      home:     kanze@gabi-soft.fr        +33 (0)1 39 55 85 62
                 office:   kanze@vx.cit.alcatel.fr   +33 (0)1 69 63 14 54
GABI Software, Sarl., 22 rue Jacques-Lemercier, F-78000 Versailles France
     -- Conseils en informatique industrielle --
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]