Re: Templates - Create a method when T is a particular type?
On 4/14/2014 2:37 PM, cpisztest@gmail.com wrote:
I am simply asking what the syntax is when implementing the methods
belonging to the specialized template, outside of the specialized
template. I already posted the code, the erroneous part was commented
out with /* and */ with // what is the syntax here?
I got it now. See below.
Here it is again without the erroneous part commented out:
//------------------------------------------------
class A
{
};
//------------------------------------------------
//------------------------------------------------
class B
{
};
//------------------------------------------------
//------------------------------------------------
template<class T>
class MyTemplateClass
{
public:
MyTemplateClass();
~MyTemplateClass();
void Foo(const T & arg);
};
All is fine so far.
//------------------------------------------------
template<class T>
MyTemplateClass<T>::MyTemplateClass()
{
}
//------------------------------------------------
template<class T>
MyTemplateClass<T>::~MyTemplateClass()
{
}
//------------------------------------------------
template<class T>
void MyTemplateClass<T>::Foo(const T & arg)
{
}
All is fine, still.
//------------------------------------------------
//------------------------------------------------
// Specialization
// So I have to redo the entire interface a second time even though I am
// only adding one new method?
template<>
class MyTemplateClass<class B>
{
public:
MyTemplateClass();
~MyTemplateClass();
void Foo(const B & arg);
void Bar(const B & arg);
So, the template specialization add a member. Totally OK.
};
//------------------------------------------------
//------------------------------------------------
// I also must do the implementation of every method even though they
// are all the same, except for adding one new one?
// and what is the syntax?
template<>
MyTemplateClass<class B>::MyTemplateClass()
{
}
//------------------------------------------------
template<class T>
Drop the 'template' here. What you're defining here is *not* a
template. It's a member of a fully specialized template, which is
actually a class. So, a member of a class is just a function, not a
template.
MyTemplateClass<double>::~MyTemplateClass()
{
}
So this needs to simply be
MyTempalateClass<double>::~MyTemplateClass() {}
//------------------------------------------------
template<class T>
void MyTemplateClass<T>::Foo(const T & arg)
{
}
This is a duplicate definition of that member. Remove it.
//------------------------------------------------
template<>
void MyTemplateClass<B>::Bar(const B & arg)
{
}
Again, since you're defining a regular member function of a full
template specialization (which is in itself a class and not a template),
there is no need to prepend it with 'template<..>', just write
void MyTemplateClass<B>::Bar(const B & arg) {}
//------------------------------------------------
int main()
{
A argTypeA;
B argTypeB;
MyTemplateClass<A> templateTypeA;
MyTemplateClass<B> templateTypeB;
templateTypeA.Foo(argTypeA);
templateTypeB.Foo(argTypeB);
return 0;
}
V
--
I do not respond to top-posted replies, please don't ask