If I have a template base class that takes the type of the derived class, is safe to cast the this pointer of this base class to the type of the derived class?
Consider this code, where the base class A
casts the this pointer to a template parameter (Derived
). It also checks if the provided type is actually derived from this class. It obviously works (here), but is it well defined?
#include <iostream>
class D;
template<typename Derived, typename T>
class A
{
public:
Derived *thisToDerived() {
static_assert(std::is_base_of< A<Derived, T>, Derived>::value, "not");
return static_cast<Derived*>(this);
}
private:
T m;
};
class D : public A<D, int>
{
private:
double d;
float f;
};
int main() {
D d;
std::cout<<"this: "<<&d<<"\nderived: "<<d.thisToDerived();
}
is_base_of< A<Derived, T>, D>
seems to be wrong. If you restrict potential derived classes to D alone, you don't need to define any templates. Perhaps you meantis_base_of< A<Derived, T>, Derived>
. – n. 1.8e9-where's-my-share m.