473,609 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

recursive type template

I have the following tree definition:

template<typena me T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully. What I'd like to do though is to use
another template like 'pair', because I might have a bunch of useful
methods already defined in 'pair':

template<typena me T>
struct tree : public pair<T, tree<T {};

This doesn't compile. I tried some forward declarations with no
success. Is there any way to do it?

Sep 28 '06 #1
19 2548
n.************@ gmail.com wrote:
I have the following tree definition:

template<typena me T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully.
Maybe, but the program has undefined behavior: standard templates may only
be instantiated with complete types. It may not show in the implementation
of std::vector in your library, but it might fail in others. The reason
that it is not guaranteed to fail is that the straight forward
implementation of std::vector only uses T* internally. Thus, you have to
put in an extra check to detect incomplete types. Some implementations of
std::vector do that.
What I'd like to do though is to use
another template like 'pair', because I might have a bunch of useful
methods already defined in 'pair':

template<typena me T>
struct tree : public pair<T, tree<T {};

This doesn't compile. I tried some forward declarations with no
success. Is there any way to do it?
Not really. You could do:

#include <utility>

template < typename T >
struct tree : public std::pair< T, tree<T>* {};

int main () {

tree<inta;

}
Best

Kai-Uwe Bux

Sep 28 '06 #2
<n.************ @gmail.comwrote in message
news:11******** **************@ b28g2000cwb.goo glegroups.com.. .
I have the following tree definition:

template<typena me T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully. What I'd like to do though is to use
another template like 'pair', because I might have a bunch of useful
methods already defined in 'pair':

template<typena me T>
struct tree : public pair<T, tree<T {};

This doesn't compile. I tried some forward declarations with no
success. Is there any way to do it?
If your 'pair' is really std::pair, and that pair has an instance of each type passed to it as a
member (I assume it does), then you've effectively got struct tree having itself as a member, which
is impossible (since that member has another instance of itself as a member, ad infinitum), and in
its base class to boot, which is even more impossible (???). This problem isn't anything to do with
templates.

Uh, are you sure that 'tree' is a kind of 'pair'?

DW
Sep 28 '06 #3

n.torrey.pi...@ gmail.com wrote:
I have the following tree definition:

template<typena me T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully. What I'd like to do though is to use
another template like 'pair', because I might have a bunch of useful
methods already defined in 'pair':

template<typena me T>
struct tree : public pair<T, tree<T {};
tree<Ttemplate class is not completely defined at the point at which
is being referred as the base class.

Not sure what you are trying to do, but try using CRTP.
Have some of the functionality you need in the base and then pass the
Derived as the templare parameter to the Base, to use it in the
std::pair that you define in the Base.

>
This doesn't compile. I tried some forward declarations with no
success. Is there any way to do it?
Sep 28 '06 #4

n.************@ gmail.com wrote:
I have the following tree definition:

template<typena me T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully.

This is because the vector contains the data pointers rather than the
data itself. So you can construct a instance of the tree class in the
memory since you know its size.
>
What I'd like to do though is to use
another template like 'pair', because I might have a bunch of useful
methods already defined in 'pair':

template<typena me T>
struct tree : public pair<T, tree<T {};

This doesn't compile. I tried some forward declarations with no
success. Is there any way to do it?
But here the compiler don't know the size of the class because the tree
in pair<T, tree<T is not define well when it is used to construct
the derived tree. So the compiler give an error.

Try this:

template<typena me T>
struct tree : public pair<T, tree<T>* {};

Bests,

Shuisheng

Sep 28 '06 #5
>I have the following tree definition:
>
template<typena me T>
struct tree {
T first;
vector<tree<T second; // branches
};
maybe this way:

template<typena me Tstruct tree
{
T first;
std::vector<tre e<T subtrees;
};
int main()
{
tree<pair<int, std::string mytree;

}
Sep 28 '06 #6
Gernot Frisch wrote:
>>I have the following tree definition:

template<typen ame T>
struct tree {
T first;
vector<tree<T second; // branches
};

maybe this way:

template<typena me Tstruct tree
{
T first;
std::vector<tre e<T subtrees;
};
That is UB according to [17.4.3.6/2] item 5:

In particular, the effects are undefined in the following cases:
...
? if an incomplete type (3.9) is used as a template argument when
instantiating a template component.
>
int main()
{
tree<pair<int, std::string mytree;

}

Best

Kai-Uwe Bux
Sep 28 '06 #7
That is UB according to [17.4.3.6/2] item 5:

In particular, the effects are undefined in the following cases:
...
? if an incomplete type (3.9) is used as a template argument when
instantiating a template component.
and if I substitute "typename" by "class"?
Sep 28 '06 #8

n.************@ gmail.com wrote:
I have the following tree definition:

template<typena me T>
struct tree {
T first;
vector<tree<T second; // branches
};

which compiles successfully. What I'd like to do though is to use
another template like 'pair', because I might have a bunch of useful
methods already defined in 'pair':

template<typena me T>
struct tree : public pair<T, tree<T {};

This doesn't compile. I tried some forward declarations with no
success. Is there any way to do it?
template<typena me T>
struct tree : public pair<T, tree<T>* {};

make the second part of pair to be a pointer.t

Sep 28 '06 #9
Gernot Frisch wrote:
>
>That is UB according to [17.4.3.6/2] item 5:

In particular, the effects are undefined in the following cases:
...
? if an incomplete type (3.9) is used as a template argument when
instantiating a template component.

and if I substitute "typename" by "class"?
The word "typename" does not occur anywhere in what you quoted. So, what
exactly do you mean?
Best

Kai-Uwe Bux
Sep 28 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2530
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1 template parameter, one can specialize the method
6
2044
by: Johan Bergman | last post by:
Hi, Maybe someone can help me with this one. The following describes a somewhat simplified version of my problem, but I think it will be sufficient. I want to use class factories (virtual constructors) which have this base class: template<class T>
7
567
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
1
2376
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive process if you want. The problem now is to make a Type list so I can specify more than one node at a time to "attach" a class to. I think I will be able to handle this but I want to run the code by you guys to see there are any major design...
4
1690
by: Piotr Filip Mieszkowski | last post by:
Hello, I like both C++ and Lisp and sometimes try to mix their ideas in the code I write. Recently I started to think about writing a pair class similar to the CONS in Lisp. (For those of you who don't know Lisp: lists in Lisp are constructed of pairs whose second element is always the next pair or NIL, a special symbol.) So I've tried something like this: template <class A, class B>
5
3359
by: Mark Stijnman | last post by:
I am trying to teach myself template metaprogramming and I have been trying to create lists of related types. I am however stuck when I want to make a template that gives me the last type in a list. I started by using a linked list of types with templates like: struct MyClass1 {}; struct MyClass2 {}; struct MyClass3 {}; struct NullType {};
9
2307
by: Christian E. Böhme | last post by:
Hello all, I ran into a little problem with recursive templates that I am not sure what it has to do with, essentially, since I am currently limited in my access to compilers (namely GCC 4.1) and until now had no chance of testing the code with others. It may be an implementation detail or even in the standard (which I have no access to, unfortunately). Or maybe I have hit one of those cases whose solutions are "undefined" by the...
3
2471
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
10
2549
by: AsheeG87 | last post by:
Hello Everyone! I have a linked list and am trying to include a recursive search. However, I am having trouble understanding how I would go about that. I don't quite understand a recursive search....would any of you be so kind to explain it to me...maybe include some examples. I would GREATLY appreciate it!!!
7
2978
by: er | last post by:
hi, could someone please help with this code? template<unsigned int N,unsigned int M> class A{ public: A(); };
0
8139
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, 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...
0
8091
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8579
Oralloy
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...
0
8555
jinu1996
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...
0
8408
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 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...
1
6064
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4098
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2540
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
0
1403
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.