Topic: Templates and multiple inheritance


Author: Gerhard Menzl <gerhard.menzl@sea.ericsson.se>
Date: 1998/11/24
Raw View
Visual C++ 5.0 rejects the following code:

   template <typename T> struct I
   {
      virtual void f (T t) = 0;
   };

   template <typename T1, typename T2> struct A : I<T1>, I<T2>
   {
      virtual void f (T1 t) {}
      virtual void f (T2 t) {}
   };

arguing that I is already a direct base class of A. This is clearly
a bug because I<T1> and I<T2> are different types unless A is
instantiated with the same type twice.

I was rather relieved to find that Visual C++ 6.0 accepted the above

code. Unfortunately, the following version:

   template <typename T> struct B {};

   template <typename T> struct I
   {
      virtual void f (B<T> b) = 0;
   };

   template <typename T1, typename T2> struct A : I<T1>, I<T2>
   {
      virtual void f (B<T1> b) {}
      virtual void f (B<T2> b) {}   // error
   };

is rejected on the grounds that A<T1, T2>::f (B<T1>) is already
defined or declared. The way I see it, this is essentially the same
bug appearing in slightly more subtle circumstances (can you spell
"quick fix"?). However, before I submit a bug report I wanted to
make sure that the code is well-formed, and that I haven't
overlooked anything.

Diab Data D-C++ 4.2 accepts both versions.

Gerhard Menzl
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@localhost.localdomain (Siemel Naran)
Date: 1998/11/24
Raw View
On 24 Nov 98 01:01:57 GMT, Gerhard Menzl <gerhard.menzl@sea.ericsson.se> wrote:

>   template <typename T> struct B {};
>
>   template <typename T> struct I
>   {
>      virtual void f (B<T> b) = 0;
>   };
>
>   template <typename T1, typename T2> struct A : I<T1>, I<T2>
>   {
>      virtual void f (B<T1> b) {}
>      virtual void f (B<T2> b) {}   // error
>   };

This code is ok.  Egcs and como accept it.  I used this test driver just
to be 100% sure.

int main()
{
     A<int,int>().f(); // error: duplicate base, duplicate f
     A<int,double>().f(B<int   >()); // ok
     A<int,double>().f(B<double>()); // ok
     A<int,double>().f(B<long  >()); // error: no matching func
}

Actually, egcs gives warnings for A<int,int> whereas I think it should
be an error:

c.c:16: warning: direct base `I<int>' inaccessible in `A<int,int>'
    due to ambiguity


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]