By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 926 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Nested (non-regular) data types

P: n/a
/*
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 Astruct Nest
{
A n_val;
Nest<pair<A,A *n_next;
};

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

template<class T>
std::ostream& operator<<(std::ostream& 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<intg;
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;
}

Nov 2 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Greg Buchholz wrote:
/*
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<pair<pair<S,S>,pair<S,S>>>' and so on.
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 Astruct Nest
{
A n_val;
Nest<pair<A,A *n_next;
};

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

template<class T>
std::ostream& operator<<(std::ostream& 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<intg;
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;
}
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.