Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Specialising a template with a templated type

Reply
Thread Tools

Specialising a template with a templated type

 
 
Pete C
Guest
Posts: n/a
 
      03-21-2006
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;
}

 
Reply With Quote
 
 
 
 
amparikh@gmail.com
Guest
Posts: n/a
 
      03-21-2006

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)
{
}
};

 
Reply With Quote
 
 
 
 
Pete C
Guest
Posts: n/a
 
      03-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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!

 
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
Specialising for iterators over contiguous memory (e.g. vector, string) Phil Endecott C++ 9 02-21-2008 06:05 AM
Specialising a template member function of a template class? Ian C++ 9 06-05-2005 01:37 AM
Template specialization for templated and primitive type template parameters case2005 C++ 3 02-13-2005 06:53 PM
Subtypes of templated types (in templated functions) Marijn C++ 5 02-13-2004 09:50 AM
implementing a templated struct within a templated struct RA Scheltema C++ 3 01-06-2004 11:25 AM



Advertisments