473,386 Members | 2,114 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,386 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 5389
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.