Re: Problem with template using base class template in GCC

From: Paul (invisiblepaul_at_hotmail.com)
Date: 08/27/04


Date: 27 Aug 2004 09:52:11 -0700


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:<81yXc.316605$a24.50596@attbi_s03>...
> "Walt Karas" <wkaras@yahoo.com> wrote...
> > The following gives an error in the declaration of the
> > member function x() of the class template Tpl, compiliing
> > with a recent version of GCC under Solaris:
> >
> > class A { };
> > class B { };
> >
> > template <typename Base>
> > class Tpl : protected Base
> > {
> > public:
> > template <typename AOrB>
> > int x(void) { return(baseX<AOrB>()); }
>
> I got it to compile by doing this:
>
> template <typename AOrB>
> inx x() { return this->template baseX<AOrB>(); }
>
> > };
> >
> > class TheBase
> > {
> > public:
> >
> > template<typename AOrB>
> > int baseX(void);
> > };
> >
> > template<>
> > inline int TheBase::baseX<A>(void) { return(1); }
> >
> > template<>
> > inline int TheBase::baseX<B>(void) { return(2); }
> >
> > Tpl<TheBase> aTpl;
> >
> > But if I give 'baseX' a dummy parameter whose type is the type parameter
> > to the template, making explicit naming of the type paramenter in the
> > baseX call unecessary, it does compile:
> >
> > class A { };
> > class B { };
> >
> > template <typename Base>
> > class Tpl : protected Base
> > {
> > public:
> > template <typename AOrB>
> > int x(void) { return(baseX(AOrB())); }
> > };
> >
> > class TheBase
> > {
> > public:
> >
> > template<typename AOrB>
> > int baseX(AOrB aOrB);
> > };
> >
> > template<>
> > inline int TheBase::baseX<A>(A a) { return(1); }
> >
> > template<>
> > inline int TheBase::baseX<B>(B b) { return(2); }
> >
> > Tpl<TheBase> aTpl;
> >
> > Is there some rule in the standard that requires this behavior?
>
> Yes, if the base class depends on the template type, its scope is not
> searched to resolve a dependent name.
>
> > Or is this just a problem with the GCC compiler?
>
> I don't think so. Comeau also rejected the first example.
>
> > Another thing that surprised me was that GCC gives me an error if
> > I put the partial specializations of 'baseX' inside the declaration
> > of the class 'TheBase'.
>
> That's also still prohibited. They are thinking of adding it to the
> next Standard, IIRC.
>
> V

Firstly I'd like to say that it compiles ok with MS 2003 Compiler.

I believe it should compile ok as I can see nothing wrong with the
code.
But the point made about name lookup , I have read too that base
templates are not searched but in this case we have the rules of
inheritance do we not. You could also try to call the base funtion
with fully qualified name which also worked for me but was not
required i.e: theBase::baseX<T>(T b);.
Try making your inheritance public then you should be able to call
baseX directly from instances of Tpl.
 I don't think it would be very good if templating a class meant we
lost the inheritance, so I believe it's a fault with your compiler.

And also you are speaking of partial specialization but this is not
partial specialization it's full specialization of member function
baseX. I'm not sure on all the rules but I can define them within the
class and it works fine. And I don't see why you shouldn't be able too
do so. I think member function specializations are generally defined
out of class for clarity though.

HTH Paul



Relevant Pages

  • Re: Non-inline template specialization of member function okay?
    ... template T Createconst; ... implemented in its CPP file: ... Member function specialization cannot be declared inside the class. ... INTERNAL COMPILER ERROR (compiler file ...
    (microsoft.public.vc.language)
  • Internal compiler error using has_member idiom for templated members
    ... compilation based on the existence of a certain member function. ... same based on the existence of a possible overload to a template member ... Next is my adapted version which causes the internal compiler error (same ...
    (microsoft.public.dotnet.languages.vc)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... templates let the compiler do more work ... The worst thing is when a function throws an exception ... And of course, exception specifications should work like people think they work, not as they /do/ work. ...
    (comp.arch.embedded)
  • Re: OOP and OOD for senior C-style embedded software engineers
    ... metaprogramming in C++ to whole different level using the template ... In particular, templates let the compiler do more work at compile time, and save the target from work at run time. ... Templates can be great for generic libraries, and do not necessarily create code bloat. ... You can use RAII even without exceptions, but obviously you have to handle your errors explicitly. ...
    (comp.arch.embedded)
  • Re: [C++] Vector - guaranteed to be contiguous?
    ... >> member is templated on the input iterators as well. ... template<typename OUTITER, typename INITER, typename INITERT> ... What happens is that the compiler sees copy and thinks, "Hey, I know copy. ... But it is a template, let me try to deduce those types. ...
    (alt.comp.lang.learn.c-cpp)