template specification

M

Marc Schellens

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
 
T

tom_usenet

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
 
M

Marc Schellens

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[/QUOTE]

Thanks Tom,

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

Any suggestions for that?
thanks,
marc
 
T

tom_usenet

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
 
M

Marc Schellens

tom_usenet said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top