C++ Logo

std-proposals

Advanced search

Re: user copy constructor

From: Ville Voutilainen <ville.voutilainen_at_[hidden]>
Date: Thu, 12 Dec 2019 22:26:36 +0200
On Thu, 12 Dec 2019 at 22:25, Matthew Woehlke via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On 12/12/2019 15.17, Mario Charest via Std-Proposals wrote:
> > Imagine a class with 20 variables, out of these variable one should not be
> > copied by the copy constructor
> >
> > class Foo {
> > std::vector<int> first;
> > std::list<int> second;
> > ...
> > void *no_copy;
> >
> > Foo (const Foo &);
> > }
> >
> > Writing the Foo copy constructor is detious, error prone and leads to
> > higher maintenance. But what if one could create the copy constructor in
> > such a way that it would tell the compiler to default copy construct all
> > variables, except one (or more)
> >
> > Foo::Foo(const Foo &) *:! *no_copy(nullptr)
> > {
> > }
> >
> > Notice the:!
> >
> > Did not put much though into the details, but wanted to first get a feel if
> > this make sense.
>
> I've done something a bit like thisĀ¹ by wrapping that particular member
> in a utility class whose copy constructor has the desired behavior. Then
> the owning class just has a strictly defaulted copy ctor.

+1.

> Given that it isn't hard to do that, I'm not sure if a language feature
> has sufficient value.

Agreed.

Received on 2019-12-12 14:29:12