C++ Logo

std-proposals

Advanced search

[std-proposals] Derived object by reference to single parameter delegated templated constructors

From: David wang <wangjufan_at_[hidden]>
Date: Thu, 9 May 2024 16:01:53 +0800
On Thu, 9 May 2024 08:06:37 +0200 Sebastian Wittmeier via Std-Proposals
<std-proposals_at_[hidden]> wrote:

>have you seen in Arthur's message that the actual problem was not calling
the base >class >constructor with a value or reference parameter, but that
you should have casted >the type of the parameter to the base class type.

I had replied to Arthur's message in *Std-Proposals Digest, Vol 62, Issue
14*.
C++ uses & to avoid type conversions, but ignores the case which
interweaves with the constructor template.
Leave it to the user to perform the type conversion?
Are programmers willing to give it up by casting every derived object to a
base object in the copy constructors in C++?

>Or was this the purpose of your template to know the type of your derived
class?

No, it is not the purpose of the template to know the type of your derived
class.
The derived relationship is rather a means to fix the problem.
C++ resolves this by reference syntax denoted by the & symbol.

>Then your code will only work with user-defined derived class copy
constructors that do not cast, and not work with a defaulted one.

*No, the code works fine in all known situations.*

>In this case why do you not use a T& parameter type of the template?
>You either have a small bug in your code or you want to do something very
special. Can >you please say the purpose of the template in the base class?

It is a constructor template which provides a way to create objects of the
class with different template arguments giving users tremendous flexibility
and code reuse.
In C++, constructor templates can indeed use reference. It's the user's
freedom.

Received on 2024-05-09 08:02:07