I'm working on a project in C++ and am having trouble understanding what members of a template class get explicitly instantiated when I explicitly instantiate the template class. I've written the following file, which I then compile using Visual C++ 2008 Express Edition's Release configuration and then pop into a disassembler.
template class test {
public:
template test(T param) {
parameter = param;
};
~test() {};
int pop();
int push();
T parameter;
};
template int test::push() {return 1;}
template int test::pop() {return 2;}
template class test;
int main() {
return 0;
}
Ignoring that this file doesn't really need templates for the moment, this compiles fine. I throw the exe into the disassembler and it tells me that test
template test::test(int);
which causes test
Answer
When the constructor is a template member function, they are not instantiated unless explicitly used.
You would see the code for the constructor if you make it a non-template member function.
template class test {
public:
/***
template test(T param) {
parameter = param;
};
***/
test(T param) : parameter(param) {}
~test() {}
int pop();
int push();
T parameter;
};
No comments:
Post a Comment