473,756 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Recursive templates

I have a template function like this:
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {
/* ... */
}

template <class T>
struct Square {
/* ... */
};
template <class T>
struct Triangle {
/* ... */
};

Now, g++ has problems recursively creating the std::vector from
Square<T>.
Are recursive templates part of C++98 (or even C++0x) or is g++ just
non-compliant?
Nov 4 '08 #1
4 5443
On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <andrey....@gma il.comwrote:
I have a template function like this:
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {
/* ... */

}

template <class T>
struct Square {
/* ... */};

template <class T>
struct Triangle {
/* ... */

};

Now, g++ has problems recursively creating the std::vector from
Square<T>.
Are recursive templates part of C++98 (or even C++0x) or is g++ just
non-compliant?
You need to forward declaring the two template class before you used
them in the function definition(or even declaration) of "recarrow"

template <class T>
struct Square;

template <class T>
struct Triangle;

And don't foget "template <class T>" for recarrow.

HTH

--
Best Regards
Barry
Nov 4 '08 #2
On Nov 4, 10:12 am, Barry <dhb2...@gmail. comwrote:
On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <andrey....@gma il.comwrote:
I have a template function like this:
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {
/* ... */
}
template <class T>
struct Square {
/* ... */};
template <class T>
struct Triangle {
/* ... */
};
Now, g++ has problems recursively creating the std::vector from
Square<T>.
Are recursive templates part of C++98 (or even C++0x) or is g++ just
non-compliant?

You need to forward declaring the two template class before you used
them in the function definition(or even declaration) of "recarrow"

template <class T>
struct Square;

template <class T>
struct Triangle;

And don't foget "template <class T>" for recarrow.
So why must I forward-declare the definition instead of the body?
Is this similar to C's
typedef struct A {
B b;
} A;
typedef struct B {
A a;
} B;
?

The actual code was:
template <class Tstruct Square {}
template <class Tstruct Triangle {}
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {}

Why must the template class be forward-declared still?
Nov 4 '08 #3
andreyvul wrote:
[...]
The actual code was:
template <class Tstruct Square {}
template <class Tstruct Triangle {}
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {}

Why must the template class be forward-declared still?
It doesn't have to be forward-declared.
After I fixed your code it looks like this:
#include <vector>
#include <utility>

template <class Tstruct Square {};
template <class Tstruct Triangle {};

template <class T>
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {return NULL;}
This compiles just fine.

If you still have problems in your real code, you might
consider doing as the FAQ asks you to: Get your problem
down to a small program with as few lines as possible
which still exhibits your problem and post this here.
Then we have something to look at, instead of blindly
guessing what your real code might look like.

Schobi
Nov 4 '08 #4
On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ16·Ö, andreyvul <andrey....@gma il.comwrote:
On Nov 4, 10:12 am, Barry <dhb2...@gmail. comwrote:
On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <andrey....@gma il.comwrote:
I have a template function like this:
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {
/* ... */
}
template <class T>
struct Square {
/* ... */};
template <class T>
struct Triangle {
/* ... */
};
Now, g++ has problems recursively creating the std::vector from
Square<T>.
Are recursive templates part of C++98 (or even C++0x) or is g++ just
non-compliant?
You need to forward declaring the two template class before you used
them in the function definition(or even declaration) of "recarrow"
template <class T>
struct Square;
template <class T>
struct Triangle;
And don't foget "template <class T>" for recarrow.

So why must I forward-declare the definition instead of the body?
Is this similar to C's
typedef struct A {
B b;} A;

typedef struct B {
A a;} B;

?

The actual code was:
template <class Tstruct Square {}
template <class Tstruct Triangle {}
std::vector<std ::pair<Square<T >, Triangle<T *
recarrow(T a, T b, int n) {}

Why must the template class be forward-declared still?
You don't have to if you define "Square" and "Triangle" before the
template
function "recarrow". And don't forget "template <class T>" before
recarrow again.

But from the title of your post -- "Recursive" , I wildly guesses that
you
want things that way.

--
Best Regards
Barry
Nov 5 '08 #5

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

Similar topics

0
1482
by: b0yce | last post by:
Hi all, I am trying to create a recursive loop transformation that remembers last position of inner loop so that it continues from that point instead of the point from where the recursion started. So if input XML is <SIMPLE> <1> <2>
7
2511
by: Rolf Kemper | last post by:
Dear All, somehow I remember that such or similar question was discussed already somewhere. But I can't find it anymore. I have a template calling itself. As long it goes deeper into the hierarchy (by the key) I can set the CurrentY parameter by itself + some constant correctly. Hence which each call the CurrentY gets bigger. But when the template reaches a leave and the caller is poped from
6
2056
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>
5
2251
by: papi1976 | last post by:
Hello to everyone I heard about a strange thing about wich i have'nt find anything on the net or books, someone called this "Curiously recursive pattern" or a strange trick to play with templates and inheritance... Dunno what is that or how it works :( Does someone has any idea about this thing???
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 {
5
3368
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
2317
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...
5
1846
by: monmonja | last post by:
Hi i'm new to xsl and i have been using smarty php templating but its just so hard to read codes in smarty/php/flash than xml/xsl/flash, i rather sacrifice speed then not being able to read code after 3 months. So my problem goes like this. I have an xml that like this <avatar> <avatarId>1</avatarId> <avatarName>MyNewAvatar</avatarName> <avatarFile> <fileName>MyNewAvatar.swf</fileName>
27
1471
by: cplusplusquestion | last post by:
I've got a recursive function, but the problem is that I need to use a very big data to test it. Always my program goes to segmentation fault. Does the problem for RECURSIVE FUNCTION comes with memory thing? Change to not recursive seems very difficult. Is there any suggestion for that?
2
2492
by: felciano | last post by:
Hello -- I am trying to use XSL to process Amazon wishlist data to sort the results by type (Apparel, then Books, then DVDs, etc). Amazon's web services chunk up results in multiple pages of fixed size, e.g. 55 items gets returned in 5 XML pages of 10 items and a 6th of 5 items. Each page is returned from a distinct URL call with a PageNum parameter. I've been trying to adapt a technique described at...
0
9455
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
10031
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
9708
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...
0
8709
agi2029
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...
1
7242
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
5140
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...
0
5302
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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
2
3354
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.