473,659 Members | 2,685 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me A>
struct NoParamBase { };

template<typena me 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<BaseWit hParam<T> > : public BaseWithParam<T >
{
Wrapper(T i)
: BaseWithParam<T >(i)
{
}
};

int main()
{
Wrapper<NoParam Base<int> > a;
Wrapper<BaseWit hParam<int> > b(1);
return 0;
}

Mar 21 '06 #1
2 1800

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<typena me A>
struct NoParamBase { };

template<typena me 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<BaseWit hParam<T> > : public BaseWithParam<T >
{
Wrapper(T i)
: BaseWithParam<T >(i)
{
}
};

int main()
{
Wrapper<NoParam Base<int> > a;
Wrapper<BaseWit hParam<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<BaseWit hParam<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<BaseWit hParam<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
3335
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
9
2305
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 same type for a given instance of the class, but different types for different instances.) #include<set> using namespace std; template<class T>
3
2726
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 Foo> class Bar { private: Foo foo;
10
3674
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 ptrAny whatever it is }
7
3456
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 trying to hide the `complexity` of template from the user interface. If you look at the code DataSet should be the object that my user manipulate. Unfortunately by doing so the object returned by DataSet::Get is a FloatingPt, so without the virtual...
6
1800
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 clever design trick that can circumvent this problem. Example: template <class Pixel_t> class Image;
3
2915
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 T,typename Alloc = std::allocator<T class CircularBuffer4 { public: typedef typename CircularBuffer<T,Alloc>::size_type
2
1920
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 following bit of code: 8<----------------------------
5
1785
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 matters to me. I have a function that is templated on an iterator type: template <typename Iter>
0
8427
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8746
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8523
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4334
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.