[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Recursive templates

andreyvul

11/4/2008 3:04:00 PM

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?
4 Answers

Barry

11/4/2008 3:13:00 PM

0

On 11?4?, ??11?04?, andreyvul <andrey....@gmail.com> wrote:
> 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

andreyvul

11/4/2008 3:17:00 PM

0

On Nov 4, 10:12 am, Barry <dhb2...@gmail.com> wrote:
> On 11?4?, ??11?04?, andreyvul <andrey....@gmail.com> wrote:
>
>
>
> > 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 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?

Hendrik Schober

11/4/2008 3:26:00 PM

0

andreyvul wrote:
> [...]
> 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?

It doesn't have to be forward-declared.
After I fixed your code it looks like this:
#include <vector>
#include <utility>

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

Barry

11/5/2008 12:36:00 AM

0

On 11?4?, ??11?16?, andreyvul <andrey....@gmail.com> wrote:
> On Nov 4, 10:12 am, Barry <dhb2...@gmail.com> wrote:
>
> > On 11?4?, ??11?04?, andreyvul <andrey....@gmail.com> wrote:
>
> > > 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 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.

--
Best Regards
Barry