Declaring Template Classes that take Multiple Nested Templates as Parameters

Discussion in 'C++' started by pagekb@gmail.com, Jun 9, 2006.

  1. Guest

    Hello,

    I'm having some difficulty compiling template classes as containers for
    other template objects. Specifically, I have a hierarchy of template
    classes that contain each other. Template class B has an instance of
    template class A, which has some base type T (usually int or double).
    However, the base type T is important to calculations in B, so I would
    like to obtain access to the type for further variable declaration
    within B. Below is a simplified version of my best attempt at the
    syntax. I'm compiling under XCode 2.0 on Mac OS X version 10.4.6 using
    GCC 4. Error messages have been commented in after lines in which they
    occur:


    //=========================================
    #include <iostream>
    #include <vector>

    template <typename T> class A
    {
    typedef T base_type;

    public:
    base_type get_item(int index)
    {
    return data_(index);
    }

    private:
    std::vector<base_type> data_;
    };

    template <template <typename T> class A> class B
    {
    typedef T base_type; // error: 'T' does not name a type
    typedef A<base_type> Atype; // error: 'base_type' was not declared
    in this scope

    public:
    base_type get_item(int index)
    {
    return data_.get_item(index);
    }

    private:
    Atype data_;

    };
    //=========================================


    Is there any way to gain access to these nested template parameters
    from within class B? Is there a better way to get this same
    functionality without intimate knowledge of A's types? Am I just
    making some syntax error in the template class declaration?

    Thanks for the help!
     
    , Jun 9, 2006
    #1
    1. Advertising

  2. Guest

    wrote:
    > Hello,
    >
    > I'm having some difficulty compiling template classes as containers for
    > other template objects. Specifically, I have a hierarchy of template
    > classes that contain each other. Template class B has an instance of
    > template class A, which has some base type T (usually int or double).
    > However, the base type T is important to calculations in B, so I would
    > like to obtain access to the type for further variable declaration
    > within B. Below is a simplified version of my best attempt at the
    > syntax. I'm compiling under XCode 2.0 on Mac OS X version 10.4.6 using
    > GCC 4. Error messages have been commented in after lines in which they
    > occur:
    >
    >
    > //=========================================
    > #include <iostream>
    > #include <vector>
    >
    > template <typename T> class A
    > {
    > typedef T base_type;
    >
    > public:
    > base_type get_item(int index)
    > {
    > return data_(index);
    > }
    >
    > private:
    > std::vector<base_type> data_;
    > };
    >
    > template <template <typename T> class A> class B
    > {
    > typedef T base_type; // error: 'T' does not name a type
    > typedef A<base_type> Atype; // error: 'base_type' was not declared
    > in this scope
    >
    > public:
    > base_type get_item(int index)
    > {
    > return data_.get_item(index);
    > }
    >
    > private:
    > Atype data_;
    >
    > };
    > //=========================================
    >
    >
    > Is there any way to gain access to these nested template parameters
    > from within class B? Is there a better way to get this same
    > functionality without intimate knowledge of A's types? Am I just
    > making some syntax error in the template class declaration?
    >
    > Thanks for the help!


    The template parameter T that you have defined in the template class B
    is really a template parameter for the template template parameter A.

    It isnt visible outside. In many ways it is just a holder when
    declaring a template template parameter and infact if you dont refer to
    it again, you dont even need to have a name for that.

    anyway try this.


    template <typename T> class A
    {
    typedef T base_type;

    public:
    base_type get_item(int index)
    {
    return data_(index);
    }

    private:
    std::vector<base_type> data_;

    };

    template < typename T, template <typename> class A> class B
    {
    typedef T base_type; // error: 'T' does not name a type

    typedef A<base_type> Atype; // error: 'base_type' was not
    declared in this scope

    public:
    base_type get_item(int index)
    {
    return data_.get_item(index);
    }
    private:
    Atype data_;
    };
     
    , Jun 9, 2006
    #2
    1. Advertising

  3. mlimber Guest

    wrote:
    > wrote:
    > > Hello,
    > >
    > > I'm having some difficulty compiling template classes as containers for
    > > other template objects. Specifically, I have a hierarchy of template
    > > classes that contain each other. Template class B has an instance of
    > > template class A, which has some base type T (usually int or double).
    > > However, the base type T is important to calculations in B, so I would
    > > like to obtain access to the type for further variable declaration
    > > within B. Below is a simplified version of my best attempt at the
    > > syntax. I'm compiling under XCode 2.0 on Mac OS X version 10.4.6 using
    > > GCC 4. Error messages have been commented in after lines in which they
    > > occur:
    > >
    > >
    > > //=========================================
    > > #include <iostream>
    > > #include <vector>
    > >
    > > template <typename T> class A
    > > {
    > > typedef T base_type;
    > >
    > > public:
    > > base_type get_item(int index)
    > > {
    > > return data_(index);
    > > }
    > >
    > > private:
    > > std::vector<base_type> data_;
    > > };
    > >
    > > template <template <typename T> class A> class B
    > > {
    > > typedef T base_type; // error: 'T' does not name a type
    > > typedef A<base_type> Atype; // error: 'base_type' was not declared
    > > in this scope
    > >
    > > public:
    > > base_type get_item(int index)
    > > {
    > > return data_.get_item(index);
    > > }
    > >
    > > private:
    > > Atype data_;
    > >
    > > };
    > > //=========================================
    > >
    > >
    > > Is there any way to gain access to these nested template parameters
    > > from within class B? Is there a better way to get this same
    > > functionality without intimate knowledge of A's types? Am I just
    > > making some syntax error in the template class declaration?
    > >
    > > Thanks for the help!

    >
    > The template parameter T that you have defined in the template class B
    > is really a template parameter for the template template parameter A.
    >
    > It isnt visible outside. In many ways it is just a holder when
    > declaring a template template parameter and infact if you dont refer to
    > it again, you dont even need to have a name for that.
    >
    > anyway try this.
    >
    >
    > template <typename T> class A
    > {
    > typedef T base_type;
    >
    > public:
    > base_type get_item(int index)
    > {
    > return data_(index);
    > }
    >
    > private:
    > std::vector<base_type> data_;
    >
    > };
    >
    > template < typename T, template <typename> class A> class B
    > {
    > typedef T base_type; // error: 'T' does not name a type
    >
    > typedef A<base_type> Atype; // error: 'base_type' was not
    > declared in this scope
    >
    > public:
    > base_type get_item(int index)
    > {
    > return data_.get_item(index);
    > }
    > private:
    > Atype data_;
    > };


    Or...

    template <typename T> class A
    {
    public:
    typedef T base_type; // now public

    base_type get_item(int index)
    {
    return data_(index);
    }
    private:
    std::vector<base_type> data_;
    };

    template <class A> class B
    {
    typedef typename A::base_type base_type;

    public:
    base_type get_item(int index)
    {
    return data_.get_item(index);
    }
    private:
    A data_;
    };

    Cheers! --M
     
    mlimber, Jun 9, 2006
    #3
    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. ian
    Replies:
    5
    Views:
    569
  2. JKop
    Replies:
    3
    Views:
    520
  3. recover
    Replies:
    2
    Views:
    856
    recover
    Jul 25, 2006
  4. A L
    Replies:
    1
    Views:
    530
    Alf P. Steinbach /Usenet
    Aug 25, 2010
  5. kito
    Replies:
    2
    Views:
    438
Loading...

Share This Page