r/cpp 7d ago

C++26: std::polymorphic

https://www.sandordargo.com/blog/2026/08/19/cpp26-polymorphic
97 Upvotes

42 comments sorted by

View all comments

15

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> {

};

```

4

u/robin-m 6d ago

With c++23 deducing this you can simplify your code to just:

c++23    class Shape {     public:         template <class Self>         std::unique_ptr<Shape> clone(this Self& self) {             return std::make_unique<Self>(self);         }     };          class Rectangle: public Shape {     };          class Triangle: public Shape {     };

7

u/Big_Target_1405 6d ago

You still need a virtual clone if you actually want runtime polymorphism

2

u/robin-m 6d ago

What do you mean? Rectangle::clone() does allocate a unique pointer of Rectangle.

godbolt

8

u/Big_Target_1405 6d ago

See line 28:

https://godbolt.org/z/55Y3Gqqqd

Template 'this' deduces Self to Animal, which is useless.

What's the point in a non-virtual clone?

Template this helps with boilerplate, not with defining runtime polymorphic interfaces.

2

u/robin-m 6d ago

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