C++ Logo

std-proposals

Advanced search

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

From: Sebastian Wittmeier <wittmeier_at_[hidden]>
Date: Thu, 9 May 2024 08:06:37 +0200
Hi David, 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.   Or was this the purpose of your template to know the type of your derived class? Then your code will only work with user-defined derived class copy constructors that do not cast, and not work with a default ed one. 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?   -----Ursprüngliche Nachricht----- Von:David wang via Std-Proposals <std-proposals_at_[hidden]> Gesendet:Do 09.05.2024 04:09 Betreff:[std-proposals] Derived object by reference to single parameter delegated templated constructors An:C++ <std-proposals_at_[hidden]>; CC:David wang <wangjufan_at_[hidden]>; On Tue, May 7, 2024 at 3:46?AM Arthur OrDwyer via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote:  >This code is buggy. But it also violates the Rule of Zero: you wrote code >*manually*, and you got it *wrong*, so naturally it's also going to do >the wrong thing.  Think about it, the code you wrote class by class is absolutely correct, I mean not breaking any rules. But some code sequences are wrong, which means I have to focus on combinations of codes, which is very bad.   >And it leads us to a specific workaround in this case. If we really must >take control of `Derived`'s value semantics ? if we can't delegate them to >some other "resource-management type" ? then we must take the >responsibility for ensuring that the correct code is run for each thing >that we manually do.  We can do it automatically for the user.  Before passing a derived object to the single parameter templated constructor, the copying process for the derived object is currently underway. The single parameter Delegated C++ constructor's copy semantics simply copy the portion declared in the derived object's base class. The portion declared in the derived class do not need to be copied or constructed again. The language's syntax is designed to express and convey the intended meaning or semantics of the code. The rules should strike a balance between syntax and semantics to better serve the intended meaning of the code. PR87310  ==============  On TWed, 8 May 2024 13:25:41 -0400 Jason McKesson via Std-Proposals <std-proposals_at_[hidden] <mailto:std-proposals_at_[hidden]> > wrote:  >I meant that `base(that)` should be `base(std::ref(that))`, and >`base`'s constructor should be changed accordingly.  `base(std::ref(that))` solves the problem via introducing another level of abstraction. And `std::ref` still can be instantiated with `T`(for example struct derived) as a reference type. struct base {   public : base() {}   template <typename T> base(T x) {} }; struct derived : public base {   public: derived() {}   derived(derived& that): base(std::ref(that)) {} }; int main() {   derived d1;   derived d2 = d1;   return 0; }   -- Std-Proposals mailing list Std-Proposals_at_[hidden] https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals

Received on 2024-05-09 06:06:40