By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,039 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,039 IT Pros & Developers. It's quick & easy.

counting nesting level in template classes

P: n/a
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 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Correction to main:

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

Apr 10 '06 #2

P: n/a
kl*****@gmail.com 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);
}
Apr 10 '06 #3

P: n/a
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 '06 #4

P: n/a
kl*****@gmail.com 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
Apr 10 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.