Specialising a template with a templated type

P

Pete C

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;
}
 
A

amparikh

Pete said:
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)
{
}
};
 
P

Pete C

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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top