C++ Logo

std-proposals

Advanced search

Re: Using `= default; ` Constructors to Remove Boilerplate Constructors

From: Alex Brachet-Mialot <alexbrachetmialot_at_[hidden]>
Date: Thu, 6 Feb 2020 14:25:24 -0500
Thanks for the comments!

> What types should this work for?

> class C {
> std::unique_ptr<int> a;

> public:
> C(std::unique_ptr<int> a) = default;
> };

> What should the compiler do?

In this case it would not work. `: a(a)` doesn't compile and the
compiler should do nothing except call copy constructors. There is no
clean way to ask that we do something special with some of the
arguments.

I had thought of something like:

// D::D(int);
class C : public D {
    std::unique_ptr<int> a;
    int b;

public:
    C(std::unique_ptr<int> a, int forD, int b) : D(forD),
a(std::move(a)) = default; // copy construct the rest of the arguments
which haven't been used.
};

But I thought that it would be too convoluted, so I don't view the
above example favorably. Of course this would never replace the
current syntax, but is just convenient for when it's useful, the same
is true for `= default` today.


On Thu, Feb 6, 2020 at 4:53 AM Viktor Kireev via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Thursday, 6 February 2020 08:00:06 MSK Alex Brachet-Mialot via Std-
> Proposals wrote:
> > Hi all,
> >
> > I just wanted to float this idea that I have had for a while.
> >
> > I find many constructors often follow this format Type(a_t a, b_t b) :
> > a(a), b(b) {}. This can get tedious quickly and look very ugly. Such
> > types can sometimes be aggregates which solves this problem, but not
> > always, aggregates have prerequisites that very few types can
> > realistically follow.
> >
> > I was thinking that `= default` could be used in this circumstance.
>
> What types should this work for?
>
> class C {
> std::unique_ptr<int> a;
>
> public:
> C(std::unique_ptr<int> a) = default;
> };
>
> What should the compiler do?
>
> --
> Victor Kireev
>
>
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2020-02-06 13:28:23