How do I make nested templates work?

Let's say I want to make a template class that take 2 types, one of which is also a template class that will depend on the first type:

1
2
3
4
5
template <sometype>
template <othertype<sometype>>
class NestedTemplateClass {
  // declaration
}


Can I do something like that? What is it called? I'd do my own reading but I don't really know what to search for...
Last edited on
http://www.cplusplus.com/doc/tutorial/templates/

1
2
3
4
template <class T, class U>
T GetMin (T a, U b) {
  return (a<b?a:b);
}

Last edited on
You can achieve something close to it, in any case:

1
2
3
4
5
template <class SomeType, template <class> class OtherType> class NestedTemplateClass
{
  OtherType<SomeType> f;
  ...
};
@Intrexa; I don't see how that helps me...

@Athar; Could you clarify how this works? Is there a name for what I'm trying to do? Let me show you my particular example:

I have a template structure called "graveyard", which takes a type of "brain" or "community" typenamed to "T":

1
2
template <typename T>
struct graveyard;


but "community" and "brain" are themselves each template structures, which take a type of TTrainer:

1
2
3
4
5
template <typename TTrainer>
struct community;

template <typename TTrainer>
struct brain;


So how do I make that first graveyard declaration work given that type "T" is itself a template?

1
2
template <typename TTrainer, typename T<TTrainer>>
struct graveyard; //?? 


How would this work given your method, as I can't see any mention of the nested (in my case TTrainer) type?
Ok I just found an example online and I think this is what I'm after:

1
2
template <typename TTrainer, template <typename TTrainer> struct T>
struct graveyard;


this is a little different to what you suggested (namely the second "typename TTrainer"); is it correct?
Last edited on
Another option is, as Intrexa suggested:

1
2
template <typename TTrainer, T>
struct graveyard;


And then simply referring to T as T<TTrainer> in the code; is that legal?
1
2
3
4
5
6
7
8
template<typename T1, template<typename T2> void f(T2) >
class Nested
{
       //...
};


template Nested<int, <double> > myClass; 


how about this one :D
Last edited on
this is a little different to what you suggested (namely the second "typename TTrainer"); is it correct?

There's no difference. Naming the nested template parameter TTrainer doesn't change anything, it's still unrelated to the first template parameter TTrainer.

And then simply referring to T as T<TTrainer> in the code; is that legal?

No, that won't work.


This is how it works:
1
2
3
4
5
6
7
template <typename TTrainer, template <typename> class T>
struct graveyard
{
  T<TTrainer> t;
};

graveyard<TrainerClass,community> g;
Last edited on
Ok, I think I understand now, thanks :)
Hmm, the following code is producing an error:

1
2
3
4
5
6
7
8
9
10
11
12
template <typename TTrainer, template <typename> typename T, template <typename> typename TParent>
struct graveyard {
	graveyard(TParent<TTrainer>*); // constructor

	TParent<TTrainer>* pTParent; // parent member of this graveyard

	T<TTrainer>* pop(); // resurrect/create T
	void push(T<TTrainer>*);  // kill T
	
	T<TTrainer>* grave[];  // array of Ts in the graveyard
	size_t grave_length; // number of pushed Ts in the graveyard
};


error C2988: unrecognizable template declaration/definition
error C2059: syntax error : '<L_TEMPLATEDECL>'
Last edited on
What if I simply had this:

1
2
3
4
5
6
7
8
9
10
11
12
template <template T, template TParent>
struct graveyard {
	graveyard(TParent*); // constructor

	TParent* pTParent; // parent member of this graveyard

	T* pop(); // resurrect/create T
	void push(T*);  // kill T
	
	T* grave[];  // array of Ts in the graveyard
	size_t grave_length; // number of pushed Ts in the graveyard
};


then if T is of type brain<TTrainer>, the line T* pop(); for example will return a type brain<TTrainer> anyway, right?
Ah right, use the class keyword (e.g. template <typename> class T).

then if T is of type brain<TTrainer>, the line T* pop(); for example will return a type brain<TTrainer> anyway, right?

Of course (assuming the two template keywords in the first line were a typo).
Last edited on
yeah, they were meant to be typename. Ok I think I've wrapped my head around it, thanks :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
template <typename TTrainer>
struct community{
  typedef TTrainer trainer_type;
};

template <typename T>
struct graveyard{
  typedef T organ_type;
  typedef typename T::trainer_type trainer_type;

  organ_type container;
  trainer_type pop();
};

graveyard< brain<president> > empty;
Sorry, I've got lost. ¿how are you planning to use it?
Topic archived. No new replies allowed.