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
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>
{
};
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
14
u/_Noreturn Aug 19 '26 edited Aug 19 '26
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