Topic: Class inheritance and explicit specialization


Author: Wolfgang Bangerth <wolf@gaia.iwr.uni-heidelberg.de>
Date: 1998/04/06
Raw View
Consider the following class hierarchy:

  template <int N>
  class Base {...};

  class Base<1> {...};


  template <int N>
  class Derived : public Base<N> {...};

Now, I ask myself whether an object of type Derived<1> would be derived
from the explicitely specialized class Base<1> or maybe if Derived<1> is
not declared unless I explicitely specialize it? I would assume that it be
the first possibility, but after some consideration I am not so sure
anymore.

The question is triggered by the following: if I have the classes

  template <int N>
  class X    { void f(); };

  class X<1> { void f(); };

then defining the template

  template <int N> void X<N>::f () {...}

refers only to the function of the general template, not to X<1>::f. If I
wanted to use the latter, I have to give an explicitely specialized
version. I consider this to be a mis-feature of the standard an use the
following workaround: put all functions which differ in implementation in
the classes Base<1>, Base<2>, ..., whereas all functions which can be
implemented in the same way independant of N are put in Derived<N>. This,
however relies upon the abovementioned feature that Derived<1> is really
derived of Base<1>, etc.

I have looked into the draft standard, but found nothing really referring
to this case. Also the recently published ARM, 3rd edition does not
mention this in detail, though I get the impression that my view may be
right when reading it.

Thanks for any clarifications on the subject,
-- Wolfgang


(By the way: gcc 2.8 and egcs both have a bug in that they look up members
of base classes of Derived<N> in Base<N>, even if N==1, i.e. you will get
a problem when using Derived<1> if Base<1> has another object layout than
Base<N>, since the compiler then will look at wrong addresses for member
variables of Base<1>. This bug is reported. There is also a workaround
for some simple cases. gcc 2.7 works correctly on this kind of class
hierarchies.)



--------------------------------------------------------------------------
Wolfgang Bangerth                          wolf@gaia.iwr.uni-heidelberg.de



--Drove_of_Donkeys_893_000--
---
[ 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              ]