C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Pass derived object by reference to single parameter delegated templated constructors

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Wed, 8 May 2024 13:25:41 -0400
On Wed, May 8, 2024 at 10:37 AM David wang via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Tue, 7 May 2024 10:31:46 -0400 Jason McKesson via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
>
> >But it *is* the user's fault.
>
> >The writer of the base class wrote a constructor with a template that
> >uses template argument deduction for a value parameter. Outside of
> >tools like `std::ref`, this makes it functionally impossible for this
> >function to be instantiated with `T` as a reference type. We must
> >therefore assume that the code inside that constructor *will not work*
> >if `T` is a reference type.
>
> >The writer of the derived class wants to call this constructor with a
> >reference type. A thing that this constructor was written to
> >explicitly disallow cannot happen and can cause it to fail.
>
> >The desires of one user conflicts with the desires of the other. When
> >user desires are conflicting, it's the users who must sort them out,
> >not the language.
>
> `std::ref` still can be instantiated with `T`(for example struct derived) as a reference type.

I meant that `base(that)` should be `base(std::ref(that))`, and
`base`'s constructor should be changed accordingly.

> No conflicts occurred between users.

No, the conflict is there. `base`'s constructor clearly said "I take
value parameters". `derived` needs to send a reference parameter. You
cannot have one without breaking the requirement of the other.

Received on 2024-05-08 17:25:53