Need info on Template specialisation

G

Ganesh

I have a question regarding template specialisation.

Assume I have a generic template class

template <class S, class T>

class A{

void f() {

// Some code here

}

}

I want to specialise the function f() for say A<int, S>.

Do I need to specialise the entire class.

If else how do i specialise just this fn/?
 
V

Victor Bazarov

Ganesh said:
I have a question regarding template specialisation.

Assume I have a generic template class

template <class S, class T>

class A{

void f() {

// Some code here

}

}

I want to specialise the function f() for say A<int, S>.

You mean for A said:
Do I need to specialise the entire class.

Yes, you do.
If else how do i specialise just this fn/?

You cannot. No partial specialisations of function templates are allowed.
A simple way would be to overload 'f' in the class.

V
 
?

??

Ganesh said:
I have a question regarding template specialisation.

Assume I have a generic template class

template <class S, class T>

class A{

void f() {

// Some code here

}

}

I want to specialise the function f() for say A<int, S>.


Do I need to specialise the entire class.

No,if you want to explicitly specialize the function:

template<>
void A<int,float>::f()
{
// blahblah
}

Still,there is no way to"partially specialize the function for the class.
If else how do i specialise just this fn/?

Just like Victor said, overload.Or make the function templated if the design
allows.
 

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,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top