If you have something like this:
#include
template class A
{
public:
void func()
{
T::func();
}
};
c
I'm not sure I understand what you're asking, but it appears you are missing the essential CRTP cast:
template
struct A {
void func() {
T& self = *static_cast(this); // CRTP cast
self.func();
}
};
struct V : A { // B for the case of virtual func
virtual void func() {
std::cout << "V::func\n";
}
};
struct NV : A { // B for the case of non-virtual func
void func() {
std::cout << "NV::func\n";
}
};
If T does not declare its own func, this will be infinite recursion as self.func will find A
Test with different final overrider to show dispatch works as advertised:
struct DV : V {
virtual void func() {
std::cout << "DV::func\n";
}
};
struct DNV : NV {
void func() {
std::cout << "DNV::func\n";
}
};
template
void call(A& a) {
a.func(); // always calls A::func
}
int main() {
DV dv;
call(dv); // uses virtual dispatch, finds DV::func
DNV dnv;
call(dnv); // no virtual dispatch, finds NV::func
return 0;
}