473,387 Members | 1,876 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.

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<typename 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<typename 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>::Type 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>::Type wibble;

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

Many thanks

Jul 23 '05 #1
3 2706
"case2005" <ca******@private.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<typename 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<typename 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>::Type wibble;
};
Here is

typename Foo<FooBar>::Type 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.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 23 '05 #2
Nicolas

Thanks for this:
typename Foo<FooBar>::Type 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<typename FooBar, template<T> class Foo>
class Bar {

private:
Foo<FooBar> foo;
}

by

Bar<char, WrappedClass> bar;
bar.foo is then of type 'WrappedClass<char>' 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>::Type 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<typename 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<typename FooBar, template<T> class Foo>
class Bar {

private:
Foo<FooBar> foo;
}

by

Bar<char, WrappedClass> bar;
bar.foo is then of type 'WrappedClass<char>' 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 PrimitiveTypeWrapper<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
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...
6
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...
2
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...
3
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: ...
3
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...
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...
12
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...
8
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
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.