Circular Templates

C

cv_curious

I have a generic container type:

template< class T > Container {};

Now I want to create a generic item class that will be 'contained'
inside the generic container. The class will change behavior according
to the container it is contained in.

template< class T > Item{};

Question: How do I tell the compiler I want an instance of container
of items? It seems there is an infinite recursion involved here.
Ideas???
 
G

Goran

I have a generic container type:

template< class T > Container {};

Now I want to create a generic item class that will be 'contained'
inside the generic container. The class will change behavior according
to the container it is contained in.

template< class T > Item{};

Question: How do I tell the compiler I want an instance of container
of items? It seems there is an infinite recursion involved here.
Ideas???

// T means "item" here
template< class T >
class Container
{
T* items_;
};

T means "container" here
template< class T >
class Item
{
void ContainerDependentStuff(T&);
};

I think the above would work. What is your problem? (posting
compilable example helps, you know...)

Goran.
 
C

cv_curious

// T means "item" here
template< class T >
class Container
{
  T* items_;

};

 T means "container" here
template< class T >
class Item
{
  void ContainerDependentStuff(T&);

};

I think the above would work. What is your problem? (posting
compilable example helps, you know...)

Goran.

The problem is how to instantiate a container of items. To progress
your example, if I now write

int main()
{

// Infinite recursion:
//Container< Item< Container< Item< ...

return 0;
}

do you see the problem I am facing???
 
B

Bart van Ingen Schenau

The problem is how to instantiate a container of items. To progress
your example, if I now write

int main()
{

// Infinite recursion:
//Container< Item< Container< Item< ...

return 0;

}

do you see the problem I am facing???

You could use a template-template parameter for your Item template:

template <template<class> C>
class Item
{
}

Usage:
Container<Item<Container> > container;

Now, Item accepts as template parameter some other template that in
turn accepts a type as template parameter.

Bart v Ingen Schenau
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top