Non-Templatized methods in Template Class?
-
This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...
CMyClass first;
CMyClass second;and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)
-
This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...
CMyClass first;
CMyClass second;and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)
This is a very good question. I do think that a function within a templated class, such as GetVersion() will indeed be implemented for each class the template gets instantiated for, creating redundand code. If this is not the case I'd really like to know from someone more knowledgable than me. You can however avoid it with various tricks, such as moving the function(s) in question out of the class definition, or into a base class that doesn't depend on a template class argument.
-
This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...
CMyClass first;
CMyClass second;and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)
I've thought about it a bit, and found something that forces the compiler to create individual methods, even if it considers the code of the entire project. What I am thinking of is Template specialization: any code using a template class may define a specialized implementation of an otherwise 'non-templatized' method, and that might happen even outside the project the compiler is looking at:
// MyClass.h
template
class MyClass {
public:
int version() const { return 1; }
};// someOtherFile_possiblyEvenWithinAnotherProject.h
#include "MyClass.h"
template <>
class MyClass {
public:
int version() const { return 2; }
}; -
This is probably a dumb question, but I was examining the vector class source code (Visual C++ 2003) and noticed some method definitions that were confusing me. After some thought, I got on this tangent about template definitions in general and started to question why all the method definitions needed to be "templatized" (for lack of a better word) if some methods never dealt directly with any "typename" for the template. In other words, if a template method (lets call it "GetVersion") did something simple like return an integer constant, irregardless of any "typename", and the class was instantiated twice, lets say...
CMyClass first;
CMyClass second;and then somewhere in the code, each instance (first and second) calls "GetVersion", I'm assuming only one copy of "GetVersion" gets created by the compiler. If this is true, why does the method definition need to "templatized"? Or Does it need to be "templatized"? The code for vector almost appears to be taking advantage of some technique that only bothers with template syntax for the methods that deal with any typename but I could easily have been deceived. Is it possible to define non-templatized methods in a template class or am I just off my rocker? Or does the compiler require template syntax for every method? (So far, I only get compiler errors if I try to omit template syntax for such methods so I'm guessing I've strayed from reality a bit here but I thought I'd ask anyway)
Of course the compiler will generate two '
GetVersion
' methods, one for each class. Please note, they have even different signature. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Of course the compiler will generate two '
GetVersion
' methods, one for each class. Please note, they have even different signature. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I've thought about it a bit, and found something that forces the compiler to create individual methods, even if it considers the code of the entire project. What I am thinking of is Template specialization: any code using a template class may define a specialized implementation of an otherwise 'non-templatized' method, and that might happen even outside the project the compiler is looking at:
// MyClass.h
template
class MyClass {
public:
int version() const { return 1; }
};// someOtherFile_possiblyEvenWithinAnotherProject.h
#include "MyClass.h"
template <>
class MyClass {
public:
int version() const { return 2; }
}; -
Yeah. I'm now realizing I really lost track of reality on this one. I think I got turned around a bit trying to think about it too much (thinking is such a dangerous pastime). C# has been rotting my brain lately so I'll blame it on that. ;)
No worries. It was a good question. I had to think for quite a time to come up with a good reason. While all you could get out of preventing the redundand template instantiation would be a few bytes less in your executable, I appreciate that some people at least consider the possibility to avoid unneccessary clutter. If all software developers were like you, Windows would run on 500 KB rather than 5 GB. :)