Template specialization

J

John

Hi,

Suppose I have a template clasee

template <class T> A
{
//data ....
T a;

//functions ....
func1(T b);
func2(T b);
func3(T b);

};

In one particular case, say int, func3 is different, so I want to use
template specialization for that case. Do I have to redefine all the
data/functions for int

template <> A<int>
{
//data ....
int a;

//functions ....
func1(int b);
func2(int b);
func3(int b);
}

even though almost everything is identical or can I only specialize the
functions that are different

template <> A<int>
{
func3(int b);
}

and still get the original generic functions/data for everything else?
My understanding is that I have to redefine everything in the
specialization, but I wanted to verify that.

Thanks,
John
 
N

Neelesh

Hi,

Suppose I have a template clasee

template <class T> A
template said:
{
   //data ....
   T a;

   //functions ....
   func1(T b);
   func2(T b);
   func3(T b);

};

In one particular case, say int, func3 is different, so I want to use
template specialization for that case.  Do I have to redefine all the
data/functions for int
Yes.
template <> A<int>
template said:
{
   //data ....
   int a;

   //functions ....
   func1(int b);
   func2(int b);
   func3(int b);

}

even though almost everything is identical or can I only specialize the
functions that are different

template <> A<int>
{
   func3(int b);

}

and still get the original generic functions/data for everything else?
My understanding is that I have to redefine everything in the
specialization, but I wanted to verify that.

Just to quote the standard: 14.7.3(1):
[Example:
template<class T> class stream;
template<> class stream<char> { /* ... */ };
Given these declarations, stream<char> will be used as the definition
of streams of chars; other
streams will be handled by class template specializations instantiated
from the class template.]
 
J

John

In one particular case, say int, func3 is different, so I want to use
template specialization for that case. Do I have to redefine all the
data/functions for int
Yes.


Just to quote the standard: 14.7.3(1):
[Example:
template<class T> class stream;
template<> class stream<char> { /* ... */ };
Given these declarations, stream<char> will be used as the definition
of streams of chars; other
streams will be handled by class template specializations instantiated
from the class template.]

Thanks,
John
 
R

Ron AF Greve

Hi,

I am not a template expert but as far as I know if you specialize the class
it just becomes a different class altoghether and you have indeed specify
everything. However maybe you can do some trick with templates and selecting
a certain template function base on the type. So adding two template
functions one for one type and one for the other (lookup SFINAE).


Regards, Ron AF Greve

http://informationsuperhighway.eu
 
J

Jerry Coffin

[email protected] says... said:
In one particular case, say int, func3 is different, so I want to
use template specialization for that case. Do I have to redefine
all the data/functions for int [ ... ] even though almost
everything is identical or can I only specialize the functions that
are different

You get that capability via inheritance rather than specialization.
In this case, you get what you want by combining the two, something
like this:

template <class T>
class A_base {
T a;
public:
void func1(T b);
void func2(T b);
};

template <class T>
class A : public A_base<T> {
public:
void func3(T b);
};

template <>
class A<int> : public A_base<int> {
public:
void func3(int b);
};
 
T

tni

You can do something like:

#include <boost/type_traits.hpp>

template<class T>
class Test {
public:
int f(T i);
};

template<class T>
int Test<T>::f(T i) {
if(boost::is_same<T, int>::value) {
return i * i;
}
else {
return i + i;
}
}


The if/else can be evaluated at compile time and will normally be
optimized away.

http://www.boost.org/doc/libs/1_39_0/libs/type_traits/doc/html/index.html
http://www.boost.org/doc/libs/1_39_.../html/boost_typetraits/reference/is_same.html

If you can't use Boost, you can also delegate to a function template or
do something like:

template<class T>
struct is_int {
static const bool v = false;
};

template<>
struct is_int<int> {
static const bool v = true;
};

template<class T>
int Test<T>::f(T i) {
if(is_int<T>::v) {
....
 

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,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top