When defining a function for a templated class, is the part in bold necessary in the function header?
Code:template <class mytype> void myclass<mytype>::myfunct(int, int) { //blah }
When defining a function for a templated class, is the part in bold necessary in the function header?
Code:template <class mytype> void myclass<mytype>::myfunct(int, int) { //blah }
Yes, I believe it is.
Why, because at that point, there is no such thing as myclass without a template, so just qualifying with myclass:: would be like qualifying with a class you never defined in the first place.
Sent from my iPadŽ
If I rember correctly the entire definition of a template class has to be within a single header file (I think it has to do with the way the compiler instatiaties templates), thus it's quite unusual for the definition of member function to be outside the main class definition.
I do believe that if you brought it outside you would have to use that sintax.
And I could be wrong about the all in 1 header part, but I've nver seen template classes split between .h and .cpp files
The other reason, is to do with "overloading" of templated types (also known as specialisation). You may have certain special cases where the generic solution that myclass provides is insufficient, and need to provide a mechanism for users of myclass to take care of those special cases, and at the same time, provide the same interface.
Hence, myclass:: alone could be ambiguous to the compiler. for example, if you had a partial specialisation which dealt with pointers:
or, maybe another class requires special attention,Code:template <mytype> void myclass<mytype*>::myfunct(int, int) { //blah };
The behaviour of the compiler is such that any call by the user of myclass will try to match up with specialisations before resorting to the generic version.Code:template <> void myclass<SomeOtherClass>::myfunct(int, int) { //blah };
Code://end waffle.
Yes. You have to qualify the function name with its class name, but there is no such thing as a class called "myclass". There will only be classes named "myclass<int>" or "myclass<double>", etc.