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: Tue, 7 May 2024 10:31:46 -0400
On Tue, May 7, 2024 at 3:46 AM David wang via Std-Proposals
<std-proposals_at_[hidden]> wrote:
>
> On Mon, 6 May 2024 10:18:35 -0400 Jason McKesson via Std-Proposals
> <std-proposals_at_[hidden]> wrote:
> >This is way too special-case to implement. It's ultimately on the user
> >to prevent this from happening. `base`'s constructor uses template
> >argument deduction. That function takes its parameter by value. If you
> >use template argument deduction on such a function and pass it a type
> >which is a reference, it will copy the parameter. This *needs* to be a
> >hard-and-fast rule.
>
> It is a special case. It is a conflict between rule and best practices.
> But It is not the user's fault. Rules and practices may change or be refined by us.

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.

Received on 2024-05-07 14:31:58