Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Recursive templates

Reply
Thread Tools

Recursive templates

 
 
andreyvul
Guest
Posts: n/a
 
      11-04-2008
I have a template function like this:
std::vector<std:air<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?
 
Reply With Quote
 
 
 
 
Barry
Guest
Posts: n/a
 
      11-04-2008
On 11月4日, 下午11时04分, andreyvul <(E-Mail Removed)> wrote:
> I have a template function like this:
> std::vector<std:air<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
 
Reply With Quote
 
 
 
 
andreyvul
Guest
Posts: n/a
 
      11-04-2008
On Nov 4, 10:12 am, Barry <(E-Mail Removed)> wrote:
> On 11月4日, 下午11时04分, andreyvul <(E-Mail Removed)> wrote:
>
>
>
> > I have a template function like this:
> > std::vector<std:air<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:air<Square<T>, Triangle<T> > > *
recarrow(T a, T b, int n) {}

Why must the template class be forward-declared still?
 
Reply With Quote
 
Barry
Guest
Posts: n/a
 
      11-05-2008
On 11月4日, 下午11时16分, andreyvul <(E-Mail Removed)> wrote:
> On Nov 4, 10:12 am, Barry <(E-Mail Removed)> wrote:
>
> > On 11月4日, 下午11时04分, andreyvul <(E-Mail Removed)> wrote:

>
> > > I have a template function like this:
> > > std::vector<std:air<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:air<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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive functions Vs Non-recursive functions - performance aspect vamsi C Programming 21 03-09-2009 10:53 PM
Two recursive calls inside of a recursive function n00m C++ 12 03-13-2008 03:18 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Comparator template argument in recursive templates? Steven T. Hatton C++ 2 10-06-2004 08:29 PM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments