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. Advertisements

  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. Advertisements

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:
    721
    Sathyaish
    May 22, 2005
  2. Brian Henry

    Data type question for OLEdb data types

    Brian Henry, Oct 4, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    711
  3. Soren Kuula
    Replies:
    2
    Views:
    803
    Henry S. Thompson
    Dec 1, 2005
  4. Jeremy Cowles

    STD types vs C++ intrinsic types

    Jeremy Cowles, Aug 18, 2003, in forum: C++
    Replies:
    5
    Views:
    2,119
    Bob Jacobs
    Aug 19, 2003
  5. Steve Knight
    Replies:
    2
    Views:
    1,046
    Steve Knight
    Oct 10, 2003
  6. ramu
    Replies:
    2
    Views:
    556
    rlblaster
    Feb 20, 2006
  7. Replies:
    11
    Views:
    1,629
    James Kanze
    Jun 7, 2007
  8. Gary Roach
    Replies:
    0
    Views:
    244
    Gary Roach
    Sep 1, 2013
Loading...