counting nesting level in template classes

K

kl.vanw

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);

}
 
K

kl.vanw

Correction to main:

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

Victor Bazarov

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);
}
 
K

kl.vanw

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

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,097
Latest member
RayE496148

Latest Threads

Top