Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > template specification

Reply
Thread Tools

template specification

 
 
Marc Schellens
Guest
Posts: n/a
 
      10-02-2003
Just to make it sure:

it is not possibel in C++ to have a template specialization
for more than one type, right?

ie. I have several template type parameters like
string, int, long, char, float, double, complex.

The template for float and double is exactly the same,
the one for the integer types as well.
So I can make the default case for the integer types, but
I have to repeat the code for the floating type specialization, right?

thanks,
marc

 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      10-02-2003
On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens
<(E-Mail Removed)> wrote:

>Just to make it sure:
>
>it is not possibel in C++ to have a template specialization
>for more than one type, right?


Right.

>
>ie. I have several template type parameters like
>string, int, long, char, float, double, complex.
>
>The template for float and double is exactly the same,
>the one for the integer types as well.
>So I can make the default case for the integer types, but
>I have to repeat the code for the floating type specialization, right?


Well, there are lots of ways you could share the code rather than
repeat it. Inheritence is one way. e.g.

template <class T>
class MyClassFloat
{
//...
};

template<>
class MyClass<float>: public FloatImpl<float> {};
template<>
class MyClass<double>: public FloatImpl<double> {};

Another way is to use indirection to the implementation through
traits, and bind float and double to the same traits.

Tom
 
Reply With Quote
 
 
 
 
Marc Schellens
Guest
Posts: n/a
 
      10-03-2003
> On Fri, 03 Oct 2003 01:51:55 +0900, Marc Schellens
> <(E-Mail Removed)> wrote:
>
>
>>Just to make it sure:
>>
>>it is not possible in C++ to have a template specialization
>>for more than one type, right?

>
>
> Right.
>
>
>>ie. I have several template type parameters like
>>string, int, long, char, float, double, complex.
>>
>>The template for float and double is exactly the same,
>>the one for the integer types as well.
>>So I can make the default case for the integer types, but
>>I have to repeat the code for the floating type specialization, right?

>
>
> Well, there are lots of ways you could share the code rather than
> repeat it. Inheritence is one way. e.g.
>
> template <class T>
> class MyClassFloat
> {
> //...
> };
>
> template<>
> class MyClass<float>: public FloatImpl<float> {};
> template<>
> class MyClass<double>: public FloatImpl<double> {};
>
> Another way is to use indirection to the implementation through
> traits, and bind float and double to the same traits.
>
> Tom


Thanks Tom,

but I was not specific enough, sorry:
I meant a member function specialization.

Any suggestions for that?
thanks,
marc

 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      10-03-2003
On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
<(E-Mail Removed)> wrote:

>but I was not specific enough, sorry:
>I meant a member function specialization.
>
>Any suggestions for that?


template<class T, bool floatimpl, bool otherimpl>
struct memfunimplchooser
{
static void run(T t)
{
//default impl
}
}

template <class T>
struct memfunimplchooser<T, true, false>
{
static void run(T t)
{
//fp impl
}
};

//etc. other impls.


//and the actual function
template <class T>
void memfun(T t)
{
memfunimplchooser<T, is_float<T>::value,
is_something_else<T>::value>::run(t);
}

Tom
 
Reply With Quote
 
Marc Schellens
Guest
Posts: n/a
 
      10-04-2003
tom_usenet wrote:
> On Fri, 03 Oct 2003 19:09:10 +0900, Marc Schellens
> <(E-Mail Removed)> wrote:
>
>
>>but I was not specific enough, sorry:
>>I meant a member function specialization.
>>
>>Any suggestions for that?

>
>
> template<class T, bool floatimpl, bool otherimpl>
> struct memfunimplchooser
> {
> static void run(T t)
> {
> //default impl
> }
> }
>
> template <class T>
> struct memfunimplchooser<T, true, false>
> {
> static void run(T t)
> {
> //fp impl
> }
> };
>
> //etc. other impls.
>
>
> //and the actual function
> template <class T>
> void memfun(T t)
> {
> memfunimplchooser<T, is_float<T>::value,
> is_something_else<T>::value>::run(t);
> }
>
> Tom


A bit ugly I think.
But thank you very much for the suggestion,
marc


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a hard problem about template specification wangjk@ihep.ac.cn C++ 10 06-20-2008 05:29 PM
template specification oddness Matthias Buelow C++ 4 02-23-2008 09:09 AM
Re: template specification oddness Victor Bazarov C++ 3 02-21-2008 07:35 PM
partial ordering of template functions & parameter specification Marek Vondrak C++ 9 05-15-2006 02:45 PM
Re: A Newbie Question about template template template Chris Theis C++ 2 07-24-2003 09:42 AM



Advertisments