Connecting Tech Pros Worldwide Help | Site Map

Template specialization for templated and primitive type template parameters

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 12:50 AM
case2005
Guest
 
Posts: n/a
Default 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


  #2  
Old July 23rd, 2005, 12:50 AM
Nicolas Pavlidis
Guest
 
Posts: n/a
Default Re: Template specialization for templated and primitive type template parameters

"case2005" <case2005@private.email.com> writes:
[color=blue]
> 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:[/color]

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.
[color=blue]
> 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.[/color]

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.

[color=blue]
> 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;
> };[/color]

Here is

typename Foo<FooBar>::Type wibble

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

[color=blue]
> I greatly appreciate any light shed on either of these issues.[/color]

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" | \|__| |
| pavnic@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
  #3  
Old July 23rd, 2005, 12:50 AM
case2005
Guest
 
Posts: n/a
Default Re: Template specialization for templated and primitive type tem

Nicolas

Thanks for this:
[color=blue]
> typename Foo<FooBar>::Type wibble[/color]

That worked. However, I don't understand:
[color=blue]
> To find out if it is a pointer type you can do
> the following:[/color]
[color=blue]
> template< typename FooBar>
> class Bar<FooBar, Foo*>
> {
> //......
> };[/color]

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.
[color=blue]
> 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.[/color]

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

  #4  
Old July 23rd, 2005, 12:50 AM
Nicolas Pavlidis
Guest
 
Posts: n/a
Default Re: Template specialization for templated and primitive type tem

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

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

This should compile.
[color=blue]
> 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.[/color]

[color=blue]
> 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?[/color]

Ok.
[color=blue]
> 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?[/color]

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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.