Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Nested (non-regular) data types

Reply
Thread Tools

Nested (non-regular) data types

 
 
Greg Buchholz
Guest
Posts: n/a
 
      11-02-2006
/*
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:stream& operator<<(std:stream& o, const std:air<T,T>& p)
{
o << "[" << p.first << "," << p.second << "]";
return o;
}

template<class T>
std:stream& operator<<(std: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,4); i.n_next =
NULL;

cout << g << endl;

return 0;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-03-2006
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 A> struct Nest
> {
> A n_val;
> Nest<pair<A,A> > *n_next;
> };
>
> template<class T>
> std:stream& operator<<(std:stream& o, const std:air<T,T>& p)
> {
> o << "[" << p.first << "," << p.second << "]";
> return o;
> }
>
> template<class T>
> std:stream& operator<<(std: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,4); 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
equivalent c data types for vc++ data types ramu C Programming 2 02-20-2006 09:33 AM
Can XSD simple types be derived from complex types? Soren Kuula XML 2 12-01-2005 07:51 PM
Where are ref types that are members of value types stored? Sathyaish ASP .Net 2 05-22-2005 07:32 PM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM
STD types vs C++ intrinsic types Jeremy Cowles C++ 5 08-19-2003 05:33 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57