5 Aug 2012 10:40
[intrusive_ptr] Why are comparison operators not friends?
Carsten Fuchs <carsten.fuchs <at> cafu.de>
2012-08-05 08:40:08 GMT
2012-08-05 08:40:08 GMT
Hi all,
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.
That is, the example program below compiles and links fine both with GCC 4.6 as well as
with Visual C++ 2010 -- but how does do the compilers match the == operator, whose right
operand is NULL, an int value?
If I try to use the exact same templated binary operators like this in my own classes,
and want NULL matched to one of the T* or U* operands, then I have to do it with friend
functions or else the compilers don't find a match.
Why does it work here?
Best regards,
Carsten
#include <boost/intrusive_ptr.hpp>
#include <iostream>
class CRefCounted
{
public:
CRefCounted() : RefCount(0) {}
long RefCount;
(Continue reading)
RSS Feed