egcs-1.0.2 Problem with definition of operator< inside template

Munagala V. S. Ramanath ram@netcom.com
Mon Apr 20 14:23:00 GMT 1998


The following program produces this error when compiled with egcs-1.0.2:

bug5.C: In method `bool PtrWrap<A>::operator <(const struct PtrWrap<A> &) const':
bug5.C:24: no match for `A & < A &'
bug5.C:24: warning: control reaches end of non-void function `PtrWrap<A>::operator <(const PtrWrap<A> &) const'

However, if the define for BAD is commented out, (the only effect this
has is to move the definition of operator< outside the class), the
diagnostic goes away. This seems like a bug since it ought not to matter
whether the definition of operator< is inside or outside the class.

Version info:
Reading specs from /d2a/exper/lib/gcc-lib/sparc-sun-sunos4.1.2/egcs-2.90.27/specs
gcc version egcs-2.90.27 980315 (egcs-1.0.2 release)

--------  CUT HERE  -----------------------------  CUT HERE  ----------
// egcs-1.0.2 produces an error message about not finding a suitable
// operator<. The diagnostic goes away if we comment out the define for
// BAD which simply moves the definition outside the class.
//
#include <fstream.h>
#include <stl.h>

// compiles cleanly if this define is commented out
#define BAD

#define NIL 0

template< class T >
struct PtrWrap {
    PtrWrap( T *p = NIL ) : ptr( p ) { }
    ~PtrWrap() { delete ptr; ptr = NIL; }

    T &operator()() const { return *ptr; }
    bool operator==( const PtrWrap &w ) const {
        return ptr == w.ptr ? true : (*ptr == *w.ptr);
    }
#ifdef BAD
    bool operator<( const PtrWrap &w ) const {
        return *ptr < *w.ptr;
    }
#else
    bool operator<( const PtrWrap &w ) const;
#endif

    T *ptr;
};

#ifndef BAD
template< class T > bool PtrWrap< T >::
operator<( const PtrWrap &w ) const {
    return *ptr < *w.ptr;
}
#endif

template< class T1, class T2 > void
PushBack( T1 *p, T2 &c )
{
    PtrWrap< T1 > wrap( p );
    c.push_back( wrap );
}

struct A;
typedef list< PtrWrap< A > > Alist;
struct B {
    float f;
    Alist::iterator aptr;
    B( float af, Alist::iterator a ) : f( af ), aptr( a ) { }
    bool operator==( const B &p ) const { return aptr == p.aptr; }
    bool operator<( const B &p ) const { return (*aptr) < (*p.aptr); }
};
struct A {
    int i;
    list< PtrWrap< B > > blist;
    A( ) { }
    A( int j ) : i( j ) { }
    bool operator==( const A &p ) const { return i == p.i; }
    bool operator<( const A &p ) const { return i < p.i; }
};

main()
{
    Alist alist;
    PushBack( new A( 9 ), alist );
    list< PtrWrap< A > >::iterator  p = alist.begin();
    PushBack( new B( 1.1, p ), (*p)().blist );

    return 0;
}




More information about the Gcc-bugs mailing list