Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2001-10-22 11:44:17


From: "David Abrahams" <david.abrahams_at_[hidden]>
> From: "Peter Dimov" <pdimov_at_[hidden]>
>
> > > > What convinced me that operator< is the right ordering primitive (in
> the
> > > > context of the current standard library design) was that I needed to
> > make
> > > a
> > > >
> > > > std::map<std::pair<typeinfo, typeinfo>, ...>
> > > >
> > > > and a specialized std::less doesn't work here, while an operator<
> does.
> > >
> > > Sorry, I must be dense. Are you saying that you define your operator<
in
> > the
> > > global namespace? Why wouldn't the standard's version of operator< for
> > pair
> > > work without intervention?
> >
> > Because std::pair<T, U>::operator< uses T::operator< and U::operator<,
not
> > less<T> and less<U>; you can't use, say, std::pair<shared_ptr, int> as a
> key
> > with only std::less<shared_ptr> defined.
>
> Okay, I get the picture, thanks.
>
> I am still confused about the details of your pair above. When you say
> typeinfo, are you referring to std::type_info, or is it just a
placeholder?

typeinfo is a simple wrapper over std::type_info const *:

namespace boost // :-)
{
 class typeinfo
 {
 public:

  typeinfo(std::type_info const & ti): ti_(&ti) {}

  char const * name() const { return ti_->name(); }

  friend inline bool operator== (typeinfo const & l, typeinfo const & r)
  {
   return *l.ti_ == *r.ti_;
  }

  friend inline bool operator!= (typeinfo const & l, typeinfo const & r)
  {
   return *l.ti_ != *r.ti_;
  }

  friend inline bool operator< (typeinfo const & l, typeinfo const & r)
  {
   return l.ti_->before(*r.ti_);
  }

 private:

  std::type_info const * ti_;
 };
}

--
Peter Dimov
Multi Media Ltd.

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