Re: Explicit template instanciation, what is incorrect
On Feb 24, 7:30 pm, Leon Pollak <le...@plris.com> wrote:
I shall be thankful for the help with the following:
Class declaration:
template <int Index, int Prio, int Dir>
class cNHI_Task : public cChnlTask, cNHI_DTS {
...
virtual void Body();
}
I should like to have two different methods "Body" for Dir=0 and Dir=1=
.. But
all my attempts like:
template <int Index, int Prio, int Dir>
void cNHI_Task<Index, Prio, 1>::Body() {}
or
template <int Index, int Prio>
void cNHI_Task<Index, Prio, 1>::Body() {}
or similar failed.
Studing the internet for hours dos not help...:(
Any comments, please?
You cannot have 2 bodies for the same function of the same template.
You would need to specialize the class template for Dir = 1 and Dir =
2 individually, in order to achieve that.
I tried making the member function Body() as a template but even that
would not work as specializing a member template without specializing
the class template is not allowed. Moreover, virtual member function
templates are not allowed.
Here is how you would specialize your class cNHI_Task:
class cChnlTask{};
class cNHI_DTS{};
template <int Index, int Prio, int Dir>
class cNHI_Task : public cChnlTask, cNHI_DTS {
public:
virtual void Body()
{
std::cout << "Dir = " << Dir << std::endl;
}
};
template <int Index, int Prio>
class cNHI_Task <Index,Prio,1> : public cChnlTask, cNHI_DTS {
public:
virtual void Body()
{
std::cout << "Specialization : Dir = " << 1 << std::endl;
}
};
template <int Index, int Prio>
class cNHI_Task <Index,Prio,2> : public cChnlTask, cNHI_DTS {
public:
virtual void Body()
{
std::cout << "Specialization : Dir = " << 2 << std::endl;
}
};
int main()
{
cNHI_Task<0,0,1> obj_dir_1;
cNHI_Task<0,0,2> obj_dir_2;
cNHI_Task<0,0,3> obj_dir_3;
obj_dir_1.Body();
obj_dir_2.Body();
obj_dir_3.Body();
}