473,837 Members | 1,657 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template specialization for templated and primitive type template parameters

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<typena me FooBar, typename Foo>
class Bar {

private:
Foo foo;
}

Now I want to be able to handle when 'Foo' is a primitive type, or more
specifically a primitive pointer type, like 'char *' and also when Foo is
itself a template class:

template<typena me FooBar, template<T> class Foo>
class Bar {

private:
Foo<FooBar> foo;
}

All the member functions and other fields of Bar are common to both
template classes. Can I do this with template specialization, if so how, I
don't quite get it.

I also tried to achieve this with the following, which isn't ideal,
because the client of the template must be aware that primitive types
must be wrapped in 'PrimitiveType' :

template<class T>
struct PrimitiveType
{
typedef T Type;
};

template<class T>
class WrappedClass
{
public:
typedef WrappedClass<T> Type;

private:
};

template<class FooBar, template<class T> class Foo>
class Bar {
public:

Foo<FooBar>::Ty pe wibble;
};

In theory, 'Bar' can have it's template parameter 'Foo' specified as
either 'PrimitiveType' or 'WrappedClass'.

Alas compilation with g++ simply gives me:

error: syntax error before `;' token

for the line Foo<FooBar>::Ty pe wibble;

I greatly appreciate any light shed on either of these issues.

Many thanks

Jul 23 '05 #1
3 2737
"case2005" <ca******@priva te.email.com> writes:
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<typena me FooBar, typename Foo>
class Bar {

private:
Foo foo;
}

Now I want to be able to handle when 'Foo' is a primitive type, or more
specifically a primitive pointer type, like 'char *' and also when Foo is
itself a template class:
To find out if it is a pointer type you can do the following:

template< typename FooBar>
class Bar<FooBar, Foo*>
{
//......
};

If foo is some pointertype hte copiler will instanciate this
specialiization .
template<typena me FooBar, template<T> class Foo>
class Bar {

private:
Foo<FooBar> foo;
}

All the member functions and other fields of Bar are common to both
template classes. Can I do this with template specialization, if so how, I
don't quite get it.
Maybe I got you wrong, but I think that's not necessary, because the
template will be instaciated with a concrete type, build by a template,
such as:

Bar<int, vector<double> >

for example. SO there is no such specialization necessary, IMHO.

I also tried to achieve this with the following, which isn't ideal,
because the client of the template must be aware that primitive types
must be wrapped in 'PrimitiveType' :

template<class T>
struct PrimitiveType
{
typedef T Type;
};

template<class T>
class WrappedClass
{
public:
typedef WrappedClass<T> Type;

private:
};

template<class FooBar, template<class T> class Foo>
class Bar {
public:

Foo<FooBar>::Ty pe wibble;
};
Here is

typename Foo<FooBar>::Ty pe wibble

required, becasue Type is a dependend name of the template Foo<...>.

I greatly appreciate any light shed on either of these issues.


I think what you are searchig for are so called typetraits:

http://www.boost.org/libs/type_traits/index.html

This library work with a lot of differnet compilers.

If you use it you can ask the compiler if a special type is a primitive
type, or not and so on, and you'd have not to write a lot of template
specializations for each type.

HTH && Kind regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tug raz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 23 '05 #2
Nicolas

Thanks for this:
typename Foo<FooBar>::Ty pe wibble
That worked. However, I don't understand:
To find out if it is a pointer type you can do
the following: template< typename FooBar>
class Bar<FooBar, Foo*>
{
//......
};
What is Foo here? I get a parse error:

error: parse error before `>' token for:

class Bar<FooBar, Foo*>

Even if this does work, it's doen't give me the polymorphism I'm looking
for in the second template argument for a primitive pointer type template
argument or a templated class as the template argument.
Maybe I got you wrong, but I think that's not
necessary, because the template will be
instaciated with a concrete type, build by a
template, such as:

Bar<int, vector<double> >

for example. SO there is no such specialization > necessary, IMHO.


No, 'Bar' accepts 'Foo' without T specified, notice that T is passed by
field 'foo' as 'FooBar'
So for example I might pass 'WrappedClass':

to

template<typena me FooBar, template<T> class Foo>
class Bar {

private:
Foo<FooBar> foo;
}

by

Bar<char, WrappedClass> bar;
bar.foo is then of type 'WrappedClass<c har>' yes?

I think what you've missed is that I want 'Bar' to
accept both a primitive pointer like 'char *' in it's second template
argument and also a templated class template argument. Do you follow?

Kind regards

Jul 23 '05 #3
case2005 wrote:
Nicolas

Thanks for this:

typename Foo<FooBar>::Ty pe wibble

That worked. However, I don't understand:

To find out if it is a pointer type you can do
the following:


template< typename FooBar>
class Bar<FooBar, Foo*>
{
//......
};

What is Foo here? I get a parse error:


Sory, I should look like this:
template<typena me FooBar, typename Foo>
class Bar<FooBar, Foo*>
{
//.....
};

This should compile.
error: parse error before `>' token for:

class Bar<FooBar, Foo*>

Even if this does work, it's doen't give me the polymorphism I'm looking
for in the second template argument for a primitive pointer type template
argument or a templated class as the template argument.
So for example I might pass 'WrappedClass':

to

template<typena me FooBar, template<T> class Foo>
class Bar {

private:
Foo<FooBar> foo;
}

by

Bar<char, WrappedClass> bar;
bar.foo is then of type 'WrappedClass<c har>' yes?
Ok.
I think what you've missed is that I want 'Bar' to
accept both a primitive pointer like 'char *' in it's second template
argument and also a templated class template argument. Do you follow?


Yes. I'm not sure, but I think that this will not work, and you'd have
to pass the hole type such as PrimitiveTypeWr apper<SomeType> to the
template, I don;t really think that specializations to template template
arguments are working.

To solve yout Problem with the pointers to primitive types:

You can specializie to each type, such as int* double* and so on, or use
the type traits, to find out the charactaristics of the type, and
produce code accroding to the results.
HTH
Nicolas
Jul 23 '05 #4

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

Similar topics

17
6871
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
6
3356
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something like that possible? Some workaround? Thank you, Patrick
2
2931
by: Nikolai Borissov | last post by:
Is it possible to specialize a templated class defined within another templeted class, like below: template<typename U> struct A { template<typename T> struct B; }; // Attempt to specialize internal class B for type int
3
362
by: Scott Frazer | last post by:
I'm trying to do some template specialization and can't get it to work quite right. I have a templated base class: template <typename T> class SignalBase { ... }; and a derived class: template <typename T> class Signal : public SignalBase<T> {
3
1751
by: mrstephengross | last post by:
Ok, I've got a templated class Foo: template<typename Tclass Foo { ; }; I want to specialize Foo for vectors holding any type: // Something like: template<typename Cclass Foo<std::vector<C { /* ... */ }; Is there any way to do this? Template template parameters, maybe?
3
2926
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
12
1882
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able to call a method defined in A's base class which at runtime determines the template parameters (I know ahead what is allowed) and calls a templated member function B with A's template parameters.
8
2971
by: flopbucket | last post by:
Hi, I want to provide a specialization of a class for any type T that is a std::map. template<typename T> class Foo { // ... };
1
2231
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
9828
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
10863
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10611
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
10263
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
9390
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...
0
6988
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
5840
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4469
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4034
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.