Circular template reference

B

Boris

I was making a part of an application more and more generic when I suddenly
ended up with code like this:

template <class T>
struct MenuItems {
T &m;
MenuItems(T &m);
};

template <class T>
struct Menu {
T items;
Menu() : items(*this) { }
};

template <class T>
MenuItems<T>::MenuItems(T &m) : m(m) { }

If these classes were no templates the code would compile without any
problems. But now with these templates it's impossible to instantiate them
with each other. You end up with something like
Menu<MenuItems<Menu<MenuItems<Menu ... Is there any trick or I went a bit
too far and have to go back to complete types?

Boris
 
S

Salt_Peter

Boris said:
I was making a part of an application more and more generic when I suddenly
ended up with code like this:

template <class T>
struct MenuItems {
T &m;
MenuItems(T &m);
};

template <class T>
struct Menu {
T items;
Menu() : items(*this) { }
};

Are you suggesting that a Menu can only hold one item?
template <class T>
MenuItems<T>::MenuItems(T &m) : m(m) { }

If these classes were no templates the code would compile without any
problems. But now with these templates it's impossible to instantiate them
with each other. You end up with something like
Menu<MenuItems<Menu<MenuItems<Menu ... Is there any trick or I went a bit
too far and have to go back to complete types?

I'm still failing to see why an item needs a pointer to its own menu /
container.
If the menu needs to somehow modify the display, thats not the item's
concern.

#include <iostream>
#include <ostream>
#include <list>
#include <iterator>

template < class T >
struct MenuItem
{
MenuItem( T obj ) : t(obj) { }
private:
T t;
/* friend op<< */
friend std::eek:stream& operator<<(std::eek:stream& os, const MenuItem< T
{
return os << r_i.t;
}
};

template< class T >
struct Menu
{
std::list< T > menuitems; // or vector, deque, set
};

template< class T >
std::eek:stream& operator<<(std::eek:stream& os, const Menu< T >& r_m)
{
std::copy( r_m.menuitems.begin(),
r_m.menuitems.end(),
std::eek:stream_iterator< T >(os,"\n") );
return os;
}

int main()
{
Menu< MenuItem< std::string > > menu;
menu.menuitems.push_back(MenuItem< std::string >("string 0"));
menu.menuitems.push_back(MenuItem< std::string >("string 1"));
menu.menuitems.push_back(MenuItem< std::string >("string 2"));

std::cout << menu;
}

/*
string 0
string 1
string 2
*/
 
V

Victor Bazarov

Boris said:
I was making a part of an application more and more generic when I
suddenly ended up with code like this:

template <class T>
struct MenuItems {
T &m;
MenuItems(T &m);
};

template <class T>
struct Menu {
T items;
Menu() : items(*this) { }
};

template <class T>
MenuItems<T>::MenuItems(T &m) : m(m) { }

If these classes were no templates the code would compile without any
problems. But now with these templates it's impossible to instantiate
them with each other. You end up with something like
Menu<MenuItems<Menu<MenuItems<Menu ... Is there any trick or I went a
bit too far and have to go back to complete types?

OK, you have found out that such circular reference is unsolvable in
C++ terms. So, what do you want from us? Your design is apparently
either too generic or not generic enough. Too generic in the sense
that you want both of those classes not be concrete. Not generic
enough in the sense that there is a dependency between them that
causes you grief. You eigher need to break the dependency or make
one of them concrete, or make both of them depend on the third type.

V
 
B

Boris

Victor said:
OK, you have found out that such circular reference is unsolvable in
C++ terms. So, what do you want from us? Your design is apparently

I got the answer to my question: It's unsolvable and I either have to change
the classes or the programming language.

Thanks,
Boris
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top