I have currently two projects, application and static library used by that project. In my static library, I have something along these lines:
// interface.h
struct IInterface {
virtual ~IInterface() = default;
virtual void foo() = 0;
};
// baseclass.h
template < class T > struct BaseClass : public IInterface {
virtual ~BaseClass() = default;
virtual void foo() override final;
protected:
virtual void onFoo() = 0;
};
template < class T > BaseClass< T >::foo() {
// some other code here
onFoo();
}
// derivedclass.h
struct SomeStruct { /* ... */ };
struct DerivedClass : public BaseClass< SomeStruct > {
protected:
virtual void onFoo() override final;
}
// derivedclass.cpp
void DerivedClass::onFoo() {
// ...
}
After I tried to compile this as static library, everything was ok. Then I tried to actually statically link this library to my application and use these classes, and following warnings and errors occurred in Visual Studio 2017:
Warning C4505 ... : unreferenced local function has been removed
which, in turn, caused linker error of unresolved external symbol
. It seems that compiler removed foo
function. Is there anything wrong with this kind of code? Why does compiler think that my function is local and unreferenced?