Boost logo

Boost :

From: Valentin Bonnard (Bonnard.V_at_[hidden])
Date: 1999-11-10 13:41:20


Darin Adler wrote:

> ivan66_at_[hidden] wrote:
> >> I assume that it would be legal to compare a shared_ptr<Derived> with a
> >> shared_ptr<Base>? Also, that a shared_array<Base> could be compared to
> >> a shared_array<Base>,
>
> Valentin Bonnard wrote:
> > It depends on the declaration of op== (member or non member).
> >
> > 1) member: conversions allowed on rhs but not lhs
> > => base == derived passes
> > but derived == base fails
> > => very strange dissymmetric effect
> >
> > 2) non member: no conversions at all
> > => symmetry in that base == derived and derived == base
> > both fail the same way
>
> Isn't it possible to have a non-member template function version of operator
> == with two class parameters which would make both base == derived and
> derived == base work?
>
> Something like this (I probably got details wrong):
>
> template <class T, class U>
> bool operator==(const shared_ptr<T>& left, const shared_ptr<U>& right)
> {
> return left.get() == right.get();
> }

Yes of course; I mean, it requires extra efforts, the simpler:

  template <class T>
  bool operator==(const shared_ptr<T>& lhs, const shared_ptr<T>& rhs);

won't allow any conversions.

-- 
Valentin Bonnard

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