Topic: templated classes with templated base classes...


Author: William Bardwell <wbardwel@platinum.com>
Date: 1998/07/24
Raw View
in 14.6.2 (in a draft of the standard) it says:
"In the definition of a class template or in the definition of a member
of such template that appears outside of the tmplate definition, if a
base class of this template depends on a template-argument, the base
class scope is not examined during name look up until the class template
is instantiated."

This appears to mean that basically a templated class with a templated
base class can't use normal syntax to get at the parent classes
variables and functions (because, unless you add a this-> or baseclass::
before them, they are considered non-dependant, and thus just end up
being not found (during use (rather than instantiation)), and syntax
errors.)  But this seems totally counter to normal inheritance syntax,
i.e. it would seem that the compiler should go looking in the base class
for the functions, find them there, know that as part of the templated
base class, they are dependant, and as such be checked instantiation
time, and work fine.

Now, I have code that is sort of like the enclosed example, and it works
fine on all the compilers I tried except for HP's aCC, and they
suggested the this-> or baseclass:: thing, citing the above part of the
standard...

Example that compiles (not runnable code) on many, but not HP's aCC:
(and will work with aCC if explicit things to make vars and func's
dependant are added)
template <class ITEM_TYPE> class a {
public:
        ITEM_TYPE *_Data;
        a(): _Data(0)
        {}
        inline void X() {
                _Data[0] = 0;
        }
};
template <class ITEM_TYPE> class b: public a<ITEM_TYPE *> {
public:
        b() {}
        ~b() {
                if (_Data) {
                        if (_Data[0])
                                delete _Data[0];
                        delete [] _Data;
                }
        }
        inline void Y() {
                X();
        }
};

int main() {
b<int> hi;
}

William Bardwell
wbardwel@platinum.com
---
[ 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: Matt Austern <austern@sgi.com>
Date: 1998/07/25
Raw View
William Bardwell <wbardwel@platinum.com> writes:

> in 14.6.2 (in a draft of the standard) it says:
> "In the definition of a class template or in the definition of a member
> of such template that appears outside of the tmplate definition, if a
> base class of this template depends on a template-argument, the base
> class scope is not examined during name look up until the class template
> is instantiated."
>
> This appears to mean that basically a templated class with a templated
> base class can't use normal syntax to get at the parent classes
> variables and functions (because, unless you add a this-> or baseclass::
> before them, they are considered non-dependant, and thus just end up
> being not found (during use (rather than instantiation)), and syntax
> errors.)

That's right.  That is correct behavior, and there are some compilers
that enforce this rule.

(And there really is a reason for this rule, incidentally.  Since
templates can be specializes, the compiler has no way of knowing
what's in the base class until it's instantiated.)

I've found that using-declarations are less painful than decorating
base class names with this-> or baseclass::.



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