Recursive templates

A

andreyvul

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?
 
B

Barry

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
 
A

andreyvul

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 T> struct Square {}
template <class T> struct 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?
 
B

Barry

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 T> struct Square {}
template <class T> struct 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top