Recursive templates

Discussion in 'C++' started by andreyvul, Nov 4, 2008.

  1. andreyvul

    andreyvul Guest

    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?
     
    andreyvul, Nov 4, 2008
    #1
    1. Advertising

  2. andreyvul

    Barry Guest

    On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <> 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
     
    Barry, Nov 4, 2008
    #2
    1. Advertising

  3. andreyvul

    andreyvul Guest

    On Nov 4, 10:12 am, Barry <> wrote:
    > On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <> 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?
     
    andreyvul, Nov 4, 2008
    #3
  4. andreyvul

    Barry Guest

    On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ16·Ö, andreyvul <> wrote:
    > On Nov 4, 10:12 am, Barry <> wrote:
    >
    > > On 11ÔÂ4ÈÕ, ÏÂÎç11ʱ04·Ö, andreyvul <> 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
     
    Barry, Nov 5, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JKop
    Replies:
    3
    Views:
    479
  2. Steven T. Hatton
    Replies:
    2
    Views:
    440
    Steven T. Hatton
    Oct 6, 2004
  3. recover
    Replies:
    2
    Views:
    813
    recover
    Jul 25, 2006
  4. n00m
    Replies:
    12
    Views:
    1,116
  5. vamsi
    Replies:
    21
    Views:
    2,083
    Keith Thompson
    Mar 9, 2009
Loading...

Share This Page