Bug in implicit conversion (gcc 3.4.1)?

Giovanni Bajo rasky@develer.com
Fri Aug 27 21:26:00 GMT 2004


Boris von Loesch wrote:

> template <class T> class BasisF;
>
> template <class T> inline bool operator<=(const BasisF<T> &a, const
> BasisF<T> &b){return a.x <= b.x;}
>
> template<class T> class BasisF{
>
> public:
>        T x;
>        BasisF(const T &_x = T()): x(_x){}
>        BasisF(const BasisF &_b): x(_b.x){}
>        ~BasisF(){}
>        BasisF& operator= (const T &_x){
>                 x = _x;
>                 return *this;
>        }
>        friend bool operator<=<>(const BasisF &a, const BasisF &b);
> };
>
> #define min(a,b) ((a) <= (b) ? (a) : (b))
>
> int main(){
>        BasisF<double> t(1);
>        if (min(t,2.0)<=0) return 1;
>        return 0;
> }

> If I move the operator implementation into the class, it works:
> template<class T> class BasisF{
> ...
>     friend bool operator<=(const...,...){ return a.x<=b.x; }
> };
>
> Is this a bug?

No, it's not a bug, GCC behaves correctly now. Your template function cannot
be matched because the deduction for the template parameter fails (and this
is before any standard conversion can be applied). Defining the inline
friend function works because it is a regular function which is pushed into
the global scope, so there is no deduction to be performed, and the compiler
will find the default conversion which makes the call possible.

The details are hairy, so I suggest you to ask somewhere else (such as the
newsgroup comp.lang.c++.moderated) for more information

Giovanni Bajo



More information about the Gcc mailing list