c++/7428: Precedence in selecting overloaded operators

topiolli@ee.oulu.fi topiolli@ee.oulu.fi
Mon Jul 29 01:58:00 GMT 2002


>Number:         7428
>Category:       c++
>Synopsis:       Precedence in selecting overloaded operators
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Jul 29 00:26:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     topiolli@ee.oulu.fi
>Release:        g++ (GCC) 3.1.1
>Organization:
>Environment:
SunOS tk3 5.8 Generic_108528-15 sun4u sparc SUNW,Ultra-80
>Description:
I have a template class called SmartPtr that does - guess what.
Since it has a typecast operator, it can be used where a
pointer to the base type is expected. Also, operator == is overloaded
to allow comparison of SmartPtr instances:

template <class T> SmartPtr
{
public:
  operator T* () { return _internalPtr; }
  operator const T* () const { return _internalPtr; }
};

template <class T> bool operator== (const SmartPtr<T>&, const SmartPtr<T>&);

The compiler chokes on this. It cannot figure out whether
it should use the overloaded operator to compare SmartPtrs,
or should it perhaps use the built-in operators for comparing
pointers, because the SmartPtr can be casted to a pointer.
The problem is easily solved by removing the overloaded
operator, but in my mind the overloaded operator should be
the preferred choice as it does not need typecasts.
>How-To-Repeat:

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list