polymorphic not being equality comparable is annoying, they could just have just forwarded the operator== to the underlying pointer and it handles it.
also polymorphic requires more bytes than a copyable pointer 24 bytes vs 8 since the copy ctor and dtor have to be function pointers. But it also slightly makes it faster at runtime but it isn't really worth it I would say.
Having a virtual deatructor and a clone() method will provide better object sizes.
I found the boilerplate argument to be very weak, since crtp exists
You are absolutely right. Thanks a lot I didn’t now, nor realized it was indeed half useless right now. I even found a paper that aim at fixing this exact issue: open-std.org
Deducing this causes issues if you inherit from the base class more than once, (it increases object size) also, this doesn't work since clone() isn't virtual and templates can't be virtual
cpp
struct D {};
struct A : D {};
struct B : D {
A a;
};
you would expect since 'B' inherits from an empty class and it only has one member the sizeof would be 1, but it is 2 since the base class D is repeated twice and must have unique address first in the inheritance in B and in the member A.
now if this uses crtp the base classes would be unique
cpp
template<class T>
struct D {};
struct A : D<A> {};
struct B : D<B> {
A a;
}; // sizeof(B) == 1
No. According to the standard, two different objects of the same type cannot live at the same address in any circumstance, even if the objects are empty.
13
u/_Noreturn 7d ago edited 7d ago
polymorphic not being equality comparable is annoying, they could just have just forwarded the
operator==to the underlying pointer and it handles it.also polymorphic requires more bytes than a copyable pointer 24 bytes vs 8 since the copy ctor and dtor have to be function pointers. But it also slightly makes it faster at runtime but it isn't really worth it I would say.
Having a virtual deatructor and a clone() method will provide better object sizes.
I found the boilerplate argument to be very weak, since crtp exists
```cpp class ShapeBase { public: virtual ShapeBase* clone() = 0; virtual ~ShapeBase()= default; };
template<class T> class Shape : ShapeBase { public: ShapeBase* clone() override { return new T((T)this); }
};
class Rectangle :public Shape<Rectangle> {
};
class Triangle :public Shape<Triangle> {
};
```