Nested (non-regular) data types

G

Greg Buchholz

/*
The program below causes my compiler to run out of memory.
I thought I'd run it past c.l.c++ to see if I made any obvious errors,
or, if this behavior was to be expected. I'm guessing the problem
comes from some sort of infinite recursion caused by the growing
type of the "Nest" data structure and the fact that a finite type
can't be statically resolved for second argument of "operator<<".
Thoughts?

Thanks,

Greg Buchholz

*/
#include<iostream>
#include<utility>

using namespace std;

template <class A> struct Nest
{
A n_val;
Nest<pair<A,A> > *n_next;
};

template<class T>
std::eek:stream& operator<<(std::eek:stream& o, const std::pair<T,T>& p)
{
o << "[" << p.first << "," << p.second << "]";
return o;
}

template<class T>
std::eek:stream& operator<<(std::eek:stream& o, const Nest<T>& p)
{
o << "{" << p.n_val ;

if(p.n_next) o << "," << *p.n_next;

return o << "}";
}

int main(int argc, char* argv[])
{
Nest<int> g;
Nest<pair<int,int> > h;
Nest<pair<pair<int,int>,pair<int,int> > > i;

g.n_val = 42; g.n_next = &h;
h.n_val = make_pair(43,44); h.n_next = &i;
i.n_val = make_pair(make_pair(45,46),make_pair(47,48)); i.n_next =
NULL;

cout << g << endl;

return 0;
}
 
V

Victor Bazarov

Greg said:
/*
The program below causes my compiler to run out of memory.

Sure. Any attempt to instantiate 'Next<S>' also requires to
instantiate 'Nest<pair<S,S>>' which then requires to instantiate
'Nest said:
I thought I'd run it past c.l.c++ to see if I made any obvious errors,
or, if this behavior was to be expected.
Expected.

I'm guessing the problem
comes from some sort of infinite recursion caused by the growing
type of the "Nest" data structure and the fact that a finite type
can't be statically resolved for second argument of "operator<<".

I don't think that operator<< has anything to do with it.
Thoughts?

None, really. Don't do it.
Thanks,

Greg Buchholz

*/
#include<iostream>
#include<utility>

using namespace std;

template <class A> struct Nest
{
A n_val;
Nest<pair<A,A> > *n_next;
};

template<class T>
std::eek:stream& operator<<(std::eek:stream& o, const std::pair<T,T>& p)
{
o << "[" << p.first << "," << p.second << "]";
return o;
}

template<class T>
std::eek:stream& operator<<(std::eek:stream& o, const Nest<T>& p)
{
o << "{" << p.n_val ;

if(p.n_next) o << "," << *p.n_next;

return o << "}";
}

int main(int argc, char* argv[])
{
Nest<int> g;
Nest<pair<int,int> > h;
Nest<pair<pair<int,int>,pair<int,int> > > i;

g.n_val = 42; g.n_next = &h;
h.n_val = make_pair(43,44); h.n_next = &i;
i.n_val = make_pair(make_pair(45,46),make_pair(47,48)); i.n_next =
NULL;

cout << g << endl;

return 0;
}
 

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,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top