Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > counting nesting level in template classes

Reply
Thread Tools

counting nesting level in template classes

 
 
kl.vanw@gmail.com
Guest
Posts: n/a
 
      04-10-2006
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);

}

 
Reply With Quote
 
 
 
 
kl.vanw@gmail.com
Guest
Posts: n/a
 
      04-10-2006
Correction to main:

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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2006
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);
}


 
Reply With Quote
 
kl.vanw@gmail.com
Guest
Posts: n/a
 
      04-10-2006
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.

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      04-10-2006
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


 
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
c is a low-level language or neither low level nor high level language pabbu C Programming 8 11-07-2005 03:05 PM
Module.nesting -> Kernel#nesting Trans Ruby 10 09-16-2005 12:21 AM
Counting template classes at compile time Patrick Kowalzick C++ 7 09-16-2004 09:32 PM
Three Level Repeater Nesting - HELP!!! Bijoy Naick ASP .Net 1 08-10-2004 07:42 PM
RE: Three Level Repeater Nesting - HELP!!! Bijoy Naick ASP .Net 0 08-10-2004 07:42 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