C++ Logo

std-proposals

Advanced search

Re: user copy constructor

From: Jake Arkinstall <jake.arkinstall_at_[hidden]>
Date: Thu, 12 Dec 2019 20:55:36 +0000
Even then, the flexibility of the initial suggestion is that a custom value
can be set upon copy. For example, the user could specify not just a
default value for the no-copy value, but also refer to the copied object,
e.g.

struct x{
     // other vars
     foo* f;
     x(x const& other)
     :!
        f{new foo(*other.f)}
     {}
};

I'd prefer a wildcard syntax, though, e.g. *{*} (but prettier), so there is
something there telling the reader that other members automatically copy.

An alternative, though one that would only work with pointers, is a version
of unique_ptr that allows copies when it holds any object with a copy
constructor. That isn't much use for other resources, mind.


On Thu, 12 Dec 2019, 20:27 Stephan Reiter via Std-Proposals, <
std-proposals_at_[hidden]> wrote:

> Can you not just mark that no_copy field as ... no copy?
>
> The following code is not complete, but illustrates the idea.
>
> #include <iostream>
> #include <string>
>
> template <typename T>
> struct NoCopy : public T
> {
> template <typename... Args>
> NoCopy(Args &&... args) : T(std::forward<Args>(args)...) {}
>
> NoCopy(const NoCopy &) : NoCopy() {}
> };
>
> struct Class
> {
> std::string a;
> NoCopy<std::string> b;
> };
>
> int main()
> {
> Class x;
> x.a = "foo";
> x.b = "bar";
> Class y = x;
> std::cout << x.a << x.b << "\n";
> std::cout << y.a << y.b << "\n";
> }
>
> Am Do., 12. Dez. 2019 um 21:17 Uhr schrieb Mario Charest via
> Std-Proposals <std-proposals_at_[hidden]>:
> >
> > Hello Everyone,
> >
> > Sorry if this has been discuss previously.
> >
> > 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.
> >
> > - Regards
> >
> > --
> > Std-Proposals mailing list
> > Std-Proposals_at_[hidden]
> > https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>

Received on 2019-12-12 14:58:13