468,101 Members | 1,325 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

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 4896
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Rolf Kemper | last post: by
6 posts views Thread by Johan Bergman | last post: by
5 posts views Thread by papi1976 | last post: by
7 posts views Thread by Jon Slaughter | last post: by
9 posts views Thread by Christian E. B枚hme | last post: by
5 posts views Thread by monmonja | last post: by
27 posts views Thread by cplusplusquestion | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.