Victor Bazarov
10/21/2008 2:41:00 PM
Hendrik Schober wrote:
> Hi,
>
> in the following code, when calling 'test_1<T>::operator==()', the
> converting ctor is invoked in order to cast a 'test_1<derived>' to
> a 'test_1<base>'. However, when calling the free function
> 'operator==(const test_2<T>&,const test_2<T>&)' both VC, GCC4.01,
> and Comeau choke, because they won't invoke the implicit conversion.
> Why isn't this allowed? What am I missing?
To invoke the free-standing function the compiler has to deduce what 'T'
is. You have two arguments, from one T would be 'base' and from the
other it would be 'derived'. The compiler has to pick one. Which one?
The conversions are not considered during template argument deduction.
>
> TIA,
>
> Schobi
>
> --8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<----8<--
>
> #include <iostream>
>
> class base {};
>
> class derived : public base {};
>
> template< typename T >
> class test_1 {
> public:
> test_1(T) {std::cout << "test_1::test_1(T)\n";}
> template< typename U >
> test_1(U) {std::cout << "test_1::test_1(U)\n";}
>
> void operator==(const test_1&) const {
> std::cout << "test_1::operator==(test_1<T>)\n";
> }
> };
>
> template< typename T >
> class test_2 {
> public:
> test_2(T) {std::cout << "test_2::test_2(T)\n";}
> template< typename U >
> test_2(U) {
> std::cout << "test_2::test_2(U)\n";
> }
> };
>
> template< typename T >
> inline void operator==(const test_2<T>&,const test_2<T>&) {
> std::cout << "operator==(test_2<T>,test_2<T>)\n";
> }
>
> int main()
> {
> base b;
> derived d;
>
> test_1<base > t1b(b);
> test_1<derived> t1d(d);
> t1b==t1d;
>
> test_2<base > t2b(b);
> test_2<derived> t2d(d);
> t2b==t2d;
>
> return 0;
> }
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask