Boost logo

Boost :

Subject: Re: [boost] [intrusive_ptr] Why are comparison operators not friends?
From: Hite, Christopher (Christopher.Hite_at_[hidden])
Date: 2012-08-09 06:47:33


> I was just (re-)reading item 46, "Define non-member functions inside templates when type conversions are desired", in Scott Meyers "Effective C++, 3rd edition", and was wondering how and why the comparison operators for intrusive_ptr work when used with NULL, even though they're > not friend functions as described in Scott Meyer's item 46.
I believe you're talking about the ancient technique of friend injection used here for example:
http://en.wikipedia.org/wiki/Barton%E2%80%93Nackman_trick

The modern good way of doing this Koenig lookup which I think deprecates that trick.
http://en.wikipedia.org/wiki/Argument-dependent_name_lookup

Just define the operator== in the namespace of the type it works on. It'll find that name in the first lookup stage and then look for a fitting overload.

In your code the first one is taken. Though I'm not sure what U is; I'm guessing void.

template<class T, class U> inline bool operator==(intrusive_ptr<T> const & a, U * b);
template<class T, class U> inline bool operator==(T * a, intrusive_ptr<U> const & b);

Chris


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