473,414 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

counting nesting level in template classes

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
4 2114
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Agent Mulder | last post by:
I have a question concerning an article by Scott Meyers, C/C++ Users Journal, April 1998, at http://www.cuj.com/documents/s=8066/cuj9804meyers/ He explains how to count objects of a given...
1
by: walexand | last post by:
I have mistake by counting a recursive element. Can someone help me. XML Sample File ---------------------------------------------------- <content> <section> <title>index1</title> <section>...
2
by: R. Heydenreich | last post by:
Hi all, I try to create an index page from a document. The idea behind is to collect a certain word and list it with *all* occurences in the document, like foo .... 12, 23, 45 bar .... 2, 5,...
7
by: Patrick Kowalzick | last post by:
Dear all, I just wondered if it is possible to count the number of classes created via a template class at compile time. To show what I mean I post an example, which is not working but carries...
3
by: shaun roe | last post by:
I have a document about 4 levels deep and in my XSLT I want to generate a unique string ID for each basic element based on its path through the hierarchy. If I use recursion, I am continually...
3
by: Philipp Schumann | last post by:
Hi, I have several nested layers of <node> element that are processed by an XSLT template. Is there any possibility to determine the depth of a node in the overall nesting hierarchy? For...
6
by: Neal | last post by:
Hi All, I used an article on XSLT and XML and creating a TOC written on the MSDN CodeCorner. ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/dncodecorn/html/corner042699.htm However, it did'nt...
1
by: PeterAPIIT | last post by:
What is template template arguments and template typename arguments ? The reason i write in policy based design is because this is the requirement of the assignment. My code so far: ...
17
by: henry | last post by:
Folks Here's a skeleton, generic HTML page, call it "index.php". You'll see a bit of php code in the middle: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.