This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Referring to a base class with template template parameters


Thanks Ian, but my code is definitely correct - lookup "template template"
parameters if you're not familiar with the syntax in my post.

Your code is different because B<T> is a final type (not sure if that's the
correct term).  In my example, class A can create a different template
instantiation of B because B is declared as a 'template template' parameter.

This code without inheritance illustrates the problem a little clearer:

//template template syntax is required to allow A::makeAnIntC() to 
//return a new template instantiation of C  
template <template <typename> class C>
class A {
	C<int> makeAnIntC() {return C<int>(5);}
};

template <typename T>
class B {
public:
	B<int> makeAnIntB() {return A::makeAnIntC();} //Compile error here
};


Note that the following will compile and work:
class Foo {
   B<int> makeAnIntB() {return A::makeAnIntC();} //Not self-referential
};


I found the following workarounds that at least get it to compile:

1.) Create a typedef outside of B:

   template <typename> class B; //forward declare B
   typedef A AB;

   B can then call: 
      return AB::makeAnIntC();

2.) This doesn't work if the type of A depends on a template parameter of B.  
     In that case, I found I could use an indirection class to declare the
type:

   //Class A now depends on another type (specified by B)
   template <typename T, template <typename> class C>
   class A;

   template <typename> class B; //forward declare B
   template <typename T>
   class Typer {
      typedef A<T,B> Type;
   } 

   B can then call:
   typedef typename Typer<T>::Type AB;
   return AB::makeAnIntC


Definitely not ideal, but it skirts around the problem for now.  This seems
like a compiler bug to me - can anyone shed some insight?

Thanks,
-aG


Ian Lance Taylor-3 wrote:
> 
> 
> I don't know why your code would work in Visual Studio.  To me it
> looks wrong in several places.
> 
> This code works in gcc, and I believe is standard conformant.
> 
> 
> template <typename T>
> class A {
> public:
>    A(int) {}
> };
> 
> template <typename T>
> class B : public A<B<T> > {
> public:
>    B(int i)
>      : A<B<T> >(i) {}
> };
> 
> 
> Ian
> 
> 

-- 
View this message in context: http://old.nabble.com/Referring-to-a-base-class-with-template-template-parameters-tp26249699p26255875.html
Sent from the gcc - Help mailing list archive at Nabble.com.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]