473,327 Members | 1,967 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,327 software developers and data experts.

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 5383
On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <andrey....@gmail.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....@gmail.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....@gmail.comwrote:
On Nov 4, 10:12 am, Barry <dhb2...@gmail.comwrote:
On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <andrey....@gmail.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
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...
7
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...
6
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...
5
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...
7
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
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...
9
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)...
5
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...
27
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...
2
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.