473,387 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Specialising a template with a templated type

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

Mar 21 '06 #1
2 1783

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 '06 #2
am******@gmail.com 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!

Mar 21 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
3
by: case2005 | last post by:
Can anyone help with the following, I don't know if it's possible, but I'm certain there must be a standard way of dealing with this. I have the following: template<typename FooBar, typename...
10
by: richardclay09 | last post by:
Please take a look at this method: template<class C> void f(C* ptrAny) { Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny); if(ptrFruit) { // do something specific to fruits } // Carry on using...
7
by: mathieu | last post by:
Hello, I did read the FAQ on template(*), since I could not find an answer to my current issue I am posting here. I have tried to summarize my issue in the following code (**). Basically I am...
6
by: Amadeus W. M. | last post by:
Sometimes - more often than I would like - I run into situations where I need a member function to be simultaneously templated and virtual. I know it can be done, but I'm wondering if there's some...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
2
by: Pierre Yves | last post by:
Hi there, Sorry for the double subject but I feel they are related. I'm not pretty sure there would be an answer but I reckon there must be a way to make it work. I would like to write the...
5
by: Phil Endecott | last post by:
Dear All, For some reason I had got it into my head that std::vector<T>::iterator and std::basic_string<T>::iterator were certain to be T*. I now see that this isn't true. So here's why this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.