In regards to give a solution for this question, I tried to simplify my code presented in the answer to use RTTI and the typeid()
function to retrieve a class name:
#include <iostream>
#include <string>
#include <typeinfo>
struct IDriver {
// Public virtual API:
virtual void func1() = 0;
// ...
virtual ~IDriver() {}
};
class SpecificDriver;
template<typename Derived>
class Driver : public IDriver {
public:
Driver() {
std::cout << typeid(*this).name() << std::endl;
std::cout << typeid(Derived).name() << std::endl;
}
virtual ~Driver() {}
};
class SpecificDriver : public Driver<SpecificDriver> {
public:
// Public virtual API:
virtual void func1();
virtual ~SpecificDriver() {}
};
int main() {
SpecificDriver sd;
}
Using this code results in a linker error:
/tmp/ccXnTrfe.o: In function `main':
main.cpp:(.text.startup+0x4f): undefined reference to `typeinfo for SpecificDriver'
Why does this result in an undefined reference error for the typeinfo
rather than the missing func1()
definition (where it's not even used BTW)?
Interestingly enough when I remove all the virtual
stuff, it works just fine:
#include <iostream>
#include <string>
#include <typeinfo>
template<typename Derived>
class Driver {
public:
Driver() {
std::cout << typeid(*this).name() << std::endl;
std::cout << typeid(Derived).name() << std::endl;
}
};
class SpecificDriver : public Driver<SpecificDriver> {
};
int main() {
SpecificDriver sd;
}
Output:
6DriverI14SpecificDriverE
14SpecificDriver
So is this really related to vtable
generation?