Specialising a template with a templated type

Discussion in 'C++' started by Pete C, Mar 21, 2006.

  1. Pete C

    Pete C Guest

    Hello...

    I am writing a templated 'wrapper' class that takes as its template
    parameter a class to be inherited from. I need a specialisation of this
    wrapper class in certain cases, however I am having trouble with the
    specialisation when its parameter is itself a templated class.

    The following code compiles with g++ 4.0.3, however Comeau says it's
    invalid. Is what I'm trying to do possible in C++, or is there another
    way to do the same thing? Thanks...


    template<typename A>
    struct NoParamBase { };

    template<typename A>
    struct BaseWithParam {
    BaseWithParam(A i) {}
    };

    template< typename T >
    struct Wrapper : public T { };

    // This attempted specialisation is the dodgy bit...
    template<>
    template <typename T>
    struct Wrapper<BaseWithParam<T> > : public BaseWithParam<T>
    {
    Wrapper(T i)
    : BaseWithParam<T>(i)
    {
    }
    };

    int main()
    {
    Wrapper<NoParamBase<int> > a;
    Wrapper<BaseWithParam<int> > b(1);
    return 0;
    }
     
    Pete C, Mar 21, 2006
    #1
    1. Advertising

  2. Pete C

    Guest

    Pete C wrote:
    > Hello...
    >
    > I am writing a templated 'wrapper' class that takes as its template
    > parameter a class to be inherited from. I need a specialisation of this
    > wrapper class in certain cases, however I am having trouble with the
    > specialisation when its parameter is itself a templated class.
    >
    > The following code compiles with g++ 4.0.3, however Comeau says it's
    > invalid. Is what I'm trying to do possible in C++, or is there another
    > way to do the same thing? Thanks...
    >
    >
    > template<typename A>
    > struct NoParamBase { };
    >
    > template<typename A>
    > struct BaseWithParam {
    > BaseWithParam(A i) {}
    > };
    >
    > template< typename T >
    > struct Wrapper : public T { };
    >
    > // This attempted specialisation is the dodgy bit...
    > template<>
    > template <typename T>
    > struct Wrapper<BaseWithParam<T> > : public BaseWithParam<T>
    > {
    > Wrapper(T i)
    > : BaseWithParam<T>(i)
    > {
    > }
    > };
    >
    > int main()
    > {
    > Wrapper<NoParamBase<int> > a;
    > Wrapper<BaseWithParam<int> > b(1);
    > return 0;
    > }


    what you are really doing is partial specialization.
    so, you need to still have the T, but the partial spec is for some
    special type of T's( which is BaseWithParam<T>)

    So what you need is really
    template< typename T>
    struct Wrapper<BaseWithParam<T> > : public BaseWithParam<T>
    {
    Wrapper(T i)
    : BaseWithParam<T>(i)
    {
    }
    };
     
    , Mar 21, 2006
    #2
    1. Advertising

  3. Pete C

    Pete C Guest

    wrote:
    > what you are really doing is partial specialization.
    > so, you need to still have the T, but the partial spec is for some
    > special type of T's( which is BaseWithParam<T>)
    >
    > So what you need is really
    > template< typename T>
    > struct Wrapper<BaseWithParam<T> > : public BaseWithParam<T>
    > {
    > Wrapper(T i)
    > : BaseWithParam<T>(i)
    > {
    > }
    > };


    I had thought that template specialisation was just pinning down some
    of the original template parameters - now I see that there is much more
    to it than that. Thankyou, I feel all educated!
     
    Pete C, Mar 21, 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. RA Scheltema
    Replies:
    3
    Views:
    424
    RA Scheltema
    Jan 6, 2004
  2. Marijn
    Replies:
    5
    Views:
    489
    Marijn
    Feb 13, 2004
  3. case2005
    Replies:
    3
    Views:
    1,860
    Nicolas Pavlidis
    Feb 13, 2005
  4. Ian
    Replies:
    9
    Views:
    452
  5. Phil Endecott
    Replies:
    9
    Views:
    493
    Abhishek Padmanabh
    Feb 21, 2008
Loading...

Share This Page