Date: Wed, 30 Apr 2025 20:36:46 +0300
On Wed, 30 Apr 2025 at 20:31, Hans Åberg <haberg_1_at_[hidden]> wrote:
>
>
> > On 30 Apr 2025, at 19:11, Ville Voutilainen <ville.voutilainen_at_[hidden]> wrote:
> >
> > On Wed, 30 Apr 2025 at 20:04, Hans Åberg via Std-Proposals
> > <std-proposals_at_[hidden]> wrote:
> >>
> >> This is a rather long video. Do you have something more succinct?
> >
> > A std::polymorphic<B>, when initialized with an in-place value of D,
> > type-erases the D but copies of the std::polymorphic copy a D.
>
> If that is enough for building my type val<A>, that is all I need. The example you gave looks right for that purpose.
It is indeed enough. And it has the advantage over a polymorphic
cloning function that a polymorphic cloning function needs
to decide how the object is allocated, whereas for use with
std::polymorphic, all your type needs to be is to be copyable,
and various different allocation strategies can be employed. That
makes the combination more flexible, while requiring
much less from your type A. You get polymorphic copies and the base
interface, and all you need to opt in to that is to
make your type copyable.
Welcome to the brave new world that type erasure makes possible, in
C++26. Stop making your polymorphic derived classes
noncopyable, make them copyable, and use them with std::polymorphic. :)
>
>
> > On 30 Apr 2025, at 19:11, Ville Voutilainen <ville.voutilainen_at_[hidden]> wrote:
> >
> > On Wed, 30 Apr 2025 at 20:04, Hans Åberg via Std-Proposals
> > <std-proposals_at_[hidden]> wrote:
> >>
> >> This is a rather long video. Do you have something more succinct?
> >
> > A std::polymorphic<B>, when initialized with an in-place value of D,
> > type-erases the D but copies of the std::polymorphic copy a D.
>
> If that is enough for building my type val<A>, that is all I need. The example you gave looks right for that purpose.
It is indeed enough. And it has the advantage over a polymorphic
cloning function that a polymorphic cloning function needs
to decide how the object is allocated, whereas for use with
std::polymorphic, all your type needs to be is to be copyable,
and various different allocation strategies can be employed. That
makes the combination more flexible, while requiring
much less from your type A. You get polymorphic copies and the base
interface, and all you need to opt in to that is to
make your type copyable.
Welcome to the brave new world that type erasure makes possible, in
C++26. Stop making your polymorphic derived classes
noncopyable, make them copyable, and use them with std::polymorphic. :)
Received on 2025-04-30 17:36:59