compiler warning at C++ template base class
- by eike
I get a compiler warning, that I don't understand in that context, when I compile the "Child.cpp" from the following code. (Don't wonder: I stripped off my class declarations to the bare minuum, so the content will not make much sense, but you will see the problem quicker). I get the warning with VS2003 and VS2008 on the highest warning level.
The code
AbstractClass.h :
#include <iostream>
template<typename T>
class AbstractClass
{
public:
virtual void Cancel(); // { std::cout << "Abstract Cancel" << std::endl; };
virtual void Process() = 0;
};
//outside definition. if I comment out this and take the inline
//definition like above (currently commented out), I don't get
//a compiler warning
template<typename T>
void AbstractClass<T>::Cancel()
{
std::cout << "Abstract Cancel" << std::endl;
}
Child.h :
#include "AbstractClass.h"
class Child : public AbstractClass<int>
{
public:
virtual void Process();
};
Child.cpp :
#include "Child.h"
#include <iostream>
void Child::Process()
{
std::cout << "Process" << std::endl;
}
The warning
The class "Child" is derived from "AbstractClass". In "AbstractClass" there's the public method "AbstractClass::Cancel()". If I define the method outside of the class body (like in the code you see), I get the compiler warning...
AbstractClass.h(7) : warning C4505: 'AbstractClass::Cancel' : unreferenced local function has been removed
with [T=int]
...when I compile "Child.cpp". I do not understand this, because this is a public function and the compiler can't know if I later reference this method or not. And, in the end, I reference this method, because I call it in main.cpp and despite this compiler warning, this method works if I compile and link all files and execute the program:
//main.cpp
#include <iostream>
#include "Child.h"
int main()
{
Child child;
child.Cancel(); //works, despite the warning
}
If I do define the Cancel() function as inline (you see it as out commented code in AbstractClass.h), then I don't get the compiler warning. Of course my program works, but I want to understand this warning or is this just a compiler mistake?
Furthermore, if do not implement AbsctractClass as a template class (just for a test purpose in this case) I also don't get the compiler warning...?