I have read a lot of posts about Curiously Recurring Template Pattern and I still do not see why I do not want to use it over just using template programming.
Below is an example slightly modified from Wikipedia:
template <class T>
struct Base
{
void interface()
{
static_cast<T*>(this)->implementation();
}
};
struct Derived : Base<Derived>
{
void implementation();
};
However, I can do exactly the same with just template in a more straightforward way:
template <class T>
struct OuterClass
{
void interface()
{
nested->implementation();
}
private:
T* nested;
};
struct NestedClass
{
void implementation();
};
OuterClass<NestedClass> x;
x.interface();
What's the advantage of CRTP over my implementation?
Edit: the line T* nested; as member variable can also be just T nested; so that nested is created by the constructor of outer class.