MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/cpp/comments/1vt2aq3/c26_stdpolymorphic/p4skt1v/?context=3
r/cpp • u/User_Deprecated • 13d ago
44 comments sorted by
View all comments
Show parent comments
3
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 12d ago You still need a virtual clone if you actually want runtime polymorphism 2 u/robin-m 12d ago What do you mean? Rectangle::clone() does allocate a unique pointer of Rectangle. godbolt 8 u/Big_Target_1405 12d 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 12d 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
7
You still need a virtual clone if you actually want runtime polymorphism
2 u/robin-m 12d ago What do you mean? Rectangle::clone() does allocate a unique pointer of Rectangle. godbolt 8 u/Big_Target_1405 12d 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 12d 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
2
What do you mean? Rectangle::clone() does allocate a unique pointer of Rectangle.
Rectangle::clone()
Rectangle
godbolt
8 u/Big_Target_1405 12d 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 12d 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
8
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 12d 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
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
3
u/robin-m 12d 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 { };