counting nesting level in template classes

Discussion in 'C++' started by kl.vanw@gmail.com, Apr 10, 2006.

  1. Guest

    I would like to count the nesting level in template classes. How can I
    make the following work?

    #include <assert.h>

    template <class T>
    class A {
    public:
    A() {
    // what goes here?
    }

    unsigned nesting_level;
    T* data;
    };

    int main() {
    A<int> one;
    A< A<int> > two;

    assert(one.order==1);
    assert(two.order==2);

    }
     
    , Apr 10, 2006
    #1
    1. Advertising

  2. Guest

    Correction to main:

    int main()
    A<int> one;
    A< A<int> > two;
    assert(one.nesting_level==1);
    assert(two.nesting_level==2);
    }
     
    , Apr 10, 2006
    #2
    1. Advertising

  3. wrote:
    > I would like to count the nesting level in template classes. How can I
    > make the following work?
    >
    > #include <assert.h>
    >
    > template <class T>
    > class A {
    > public:
    > A() {
    > // what goes here?


    Nothing. Initialise your 'data' in the initialiser list.

    > }
    >
    > unsigned nesting_level;


    Shouldn't this be 'enum' or 'static'? I think you need to initialise
    the 'nesting_level' here from 'T's "nesting_level" if any.

    Is this homework?

    Try to create a template to "get the nesting level" and implement it so
    that it returns 0 for any classes except A and returns A's nesting level
    for the A class (template).

    For the spoiler, look several pages below my signature. If this is in
    fact your homework and you value education, don't look.

    > T* data;
    > };
    >
    > int main() {
    > A<int> one;
    > A< A<int> > two;
    >
    > assert(one.nesting_level==1);
    > assert(two.nesting_level==2);
    >
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask





































































































































































    #include <assert.h>

    template <class T> struct get_n_l { enum { value = 0 }; };

    template <class T>
    class A {
    public:
    A() : data(0) {}
    enum { nesting_level = get_n_l<T>::value + 1 };
    T* data;
    };

    template <class T> struct get_n_l <A<T> > {
    enum { value = A<T>::nesting_level };
    };

    int main() {
    A<int> one;
    A< A<int> > two;

    assert(one.nesting_level == 1);
    assert(two.nesting_level == 2);
    }
     
    Victor Bazarov, Apr 10, 2006
    #3
  4. Guest

    Thanks, that does the trick. Though I'm having trouble understanding
    it. I'm just learning C++ for my research work. No, this isn't for
    homework. I have my PhD and hope to never have any homework again.
     
    , Apr 10, 2006
    #4
  5. wrote:
    > Thanks, that does the trick. Though I'm having trouble understanding
    > it.


    What exactly do you have trouble understanding?

    > I'm just learning C++ [..]


    Then trouble understanding is generally expected. C++ templates,
    partial specialisation of templates, and related topics, are part of
    the "advanced" portion of learning C++. Get a good book. I strongly
    recommend "C++ Templates" by Vandevoorde and Josuttis.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 10, 2006
    #5
    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. Bijoy Naick

    Three Level Repeater Nesting - HELP!!!

    Bijoy Naick, Aug 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,888
    Bijoy Naick
    Aug 10, 2004
  2. Bijoy Naick

    RE: Three Level Repeater Nesting - HELP!!!

    Bijoy Naick, Aug 10, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    388
    Bijoy Naick
    Aug 10, 2004
  3. pabbu
    Replies:
    8
    Views:
    757
    Marc Boyer
    Nov 7, 2005
  4. Trans
    Replies:
    10
    Views:
    319
    Sean O'Halpin
    Sep 16, 2005
  5. Kenosis
    Replies:
    3
    Views:
    163
    Robert Klemme
    Jun 12, 2006
Loading...

Share This Page