Nested (non-regular) data types

Discussion in 'C++' started by Greg Buchholz, Nov 2, 2006.

  1. /*
    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;
    }
     
    Greg Buchholz, Nov 2, 2006
    #1
    1. Advertising

  2. 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::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;
    > }


    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 3, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sathyaish
    Replies:
    2
    Views:
    590
    Sathyaish
    May 22, 2005
  2. ramu
    Replies:
    2
    Views:
    328
    rlblaster
    Feb 20, 2006
  3. Replies:
    11
    Views:
    1,405
    James Kanze
    Jun 7, 2007
  4. Gary Roach
    Replies:
    0
    Views:
    114
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    122
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page