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.orde r==1);
assert(two.orde r==2);
} 4 2143
Correction to main:
int main()
A<int> one;
A< A<int> > two;
assert(one.nest ing_level==1);
assert(two.nest ing_level==2);
} kl*****@gmail.c om 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_le vel" 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.nest ing_level==1); assert(two.nest ing_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>::val ue + 1 };
T* data;
};
template <class T> struct get_n_l <A<T> > {
enum { value = A<T>::nesting_l evel };
};
int main() {
A<int> one;
A< A<int> > two;
assert(one.nest ing_level == 1);
assert(two.nest ing_level == 2);
}
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. kl*****@gmail.c om 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 type. Inheritance is preferred
over containment, but then the "old virtual destructor spiel" comes up. Read
it. Now my silly question is, why is the destructor not declared virtual in
the template?
|
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>
<title>index11</title>
<section>
|
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, 88
and so on. I have a XML document which contains entries with different
classes (Java classes).
|
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 the
idea:
static int counter = 0; // this variable can be changed only at runtime...
template <typename T> struct want_to_be_counted;
|
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 accessing the root element ID, here
is a typical call:
<xsl:variable name="fullPath"
select="concat('p',../../../@id,'_c',../../@id,'_r',../@id,'_s',$slaveID)
"/>
| |
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 example, I would like to obtain 0 for the
root <node> tag; 1 for all child <node> tags; 2 for all tags that are child
<node> tags of all previous <node> tags (those of level 1); and so on.
Is there any way to do this?
Many thanks,
|
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 quite answer all my questions.
How would one create a 3 level TOC when each item level / node was
differently named (They used Template match and for-each, but the template
match worked as on a 3 level structure they usedf the same named xml...
|
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:
#include "StoragePolicy.h"
|
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"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |