Topic: Defaulted copy/move constructors/assignment


Author: Igor Baidiuk <target.san@gmail.com>
Date: Wed, 20 May 2015 06:34:29 -0700 (PDT)
Raw View
------=_Part_4214_1902761116.1432128869455
Content-Type: multipart/alternative;
 boundary="----=_Part_4215_1527089135.1432128869455"

------=_Part_4215_1527089135.1432128869455
Content-Type: text/plain; charset=UTF-8

You can achieve this by wrapping into protected/private structure, where
all your fields are stored. Then adding default copy/move/assignment to
that struct and doing desired thing in one piece.

On Wednesday, May 20, 2015 at 8:07:47 AM UTC+3, Will Cassella wrote:
>
> I ran into this issue the other day when writing a large class that needs
> additional book-keeping logic when being moved or copied. All fields could
> be copied/moved normally (just as the default versions would do), there was
> just a few extra function calls that needed to be made to sort things out.
> Whenever I added/removed fields in the class, I would have to reflect those
> changes in all my constructors, which became both bug-prone and a hassle.
> If there was a simple way to create a default move/copy constructor with
> some additional behavior in the body, that would be wonderful. Something
> along the lines of this:
>
> MyType::MyType(const MyType& copy)
>     : default(copy)
> {
>     // Additional logic
> }
>
> The constructor would then copy-construct all fields automatically, as
> well as running the additional code in the body. A copy-assignment operator
> could be implemented as such:
>
> MyType& operator=(const MyType& copy)
> {
>     if (this != &copy)
>     {
>         default(copy);
>         // Addition logic
>     }
>     return *this;
> }
>
> Both examples could be easily extended to move construction/assignment as
> well.
>
>

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_4215_1527089135.1432128869455
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">You can achieve this by wrapping into protected/private st=
ructure, where all your fields are stored. Then adding default copy/move/as=
signment to that struct and doing desired thing in one piece.<br><br>On Wed=
nesday, May 20, 2015 at 8:07:47 AM UTC+3, Will Cassella wrote:<blockquote c=
lass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px=
 #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">I ran into this issue the =
other day when writing a large class that needs additional book-keeping log=
ic when being moved or copied. All fields could be copied/moved normally (j=
ust as the default versions would do), there was just a few extra function =
calls that needed to be made to sort things out. Whenever I added/removed f=
ields in the class, I would have to reflect those changes in all my constru=
ctors, which became both bug-prone and a hassle. If there was a simple way =
to create a default move/copy constructor with some additional behavior in =
the body, that would be wonderful. Something along the lines of this:<br><b=
r><div style=3D"background-color:rgb(250,250,250);border-color:rgb(187,187,=
187);border-style:solid;border-width:1px;word-wrap:break-word"><code><div><=
span style=3D"color:#606">MyType</span><span style=3D"color:#660">::</span>=
<span style=3D"color:#606">MyType</span><span style=3D"color:#660">(</span>=
<span style=3D"color:#008">const</span><span style=3D"color:#000"> </span><=
span style=3D"color:#606">MyType</span><span style=3D"color:#660">&amp;</sp=
an><span style=3D"color:#000"> copy</span><span style=3D"color:#660">)</spa=
n><span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:=
#660">:</span><span style=3D"color:#000"> </span><span style=3D"color:#008"=
>default</span><span style=3D"color:#660">(</span><span style=3D"color:#000=
">copy</span><span style=3D"color:#660">)</span><span style=3D"color:#000">=
<br></span><span style=3D"color:#660">{</span><span style=3D"color:#000"><b=
r>&nbsp; &nbsp; </span><span style=3D"color:#800">// Additional logic</span=
><span style=3D"color:#000"><br></span><span style=3D"color:#660">}</span><=
span style=3D"color:#000"><br></span></div></code></div><br>The constructor=
 would then copy-construct all fields automatically, as well as running the=
 additional code in the body. A copy-assignment operator could be implement=
ed as such:<br><br><div style=3D"background-color:rgb(250,250,250);border-c=
olor:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break-w=
ord"><code><div><span style=3D"color:#606">MyType</span><span style=3D"colo=
r:#660">&amp;</span><span style=3D"color:#000"> </span><span style=3D"color=
:#008">operator</span><span style=3D"color:#660">=3D(</span><span style=3D"=
color:#008">const</span><span style=3D"color:#000"> </span><span style=3D"c=
olor:#606">MyType</span><span style=3D"color:#660">&amp;</span><span style=
=3D"color:#000"> copy</span><span style=3D"color:#660">)</span><span style=
=3D"color:#000"><br></span><span style=3D"color:#660">{</span><span style=
=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:#008">if</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#660">(</span><sp=
an style=3D"color:#008">this</span><span style=3D"color:#000"> </span><span=
 style=3D"color:#660">!=3D</span><span style=3D"color:#000"> </span><span s=
tyle=3D"color:#660">&amp;</span><span style=3D"color:#000">copy</span><span=
 style=3D"color:#660">)</span><span style=3D"color:#000"><br>&nbsp; &nbsp; =
</span><span style=3D"color:#660">{</span><span style=3D"color:#000"><br>&n=
bsp; &nbsp; &nbsp; &nbsp; </span><span style=3D"color:#008">default</span><=
span style=3D"color:#660">(</span><span style=3D"color:#000">copy</span><sp=
an style=3D"color:#660">);</span><span style=3D"color:#000"><br>&nbsp; &nbs=
p; &nbsp; &nbsp; </span><span style=3D"color:#800">// Addition logic</span>=
<span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:#6=
60">}</span><span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=
=3D"color:#008">return</span><span style=3D"color:#000"> </span><span style=
=3D"color:#660">*</span><span style=3D"color:#008">this</span><span style=
=3D"color:#660">;</span><span style=3D"color:#000"><br></span><span style=
=3D"color:#660">}</span><span style=3D"color:#000"><br></span></div></code>=
</div><br>Both examples could be easily extended to move construction/assig=
nment as well.<br><br></div></blockquote></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_4215_1527089135.1432128869455--
------=_Part_4214_1902761116.1432128869455--

.


Author: Will Cassella <wcassella@gmail.com>
Date: Wed, 20 May 2015 08:52:17 -0700 (PDT)
Raw View
------=_Part_4111_1538363890.1432137137264
Content-Type: multipart/alternative;
 boundary="----=_Part_4112_1726272997.1432137137264"

------=_Part_4112_1726272997.1432137137264
Content-Type: text/plain; charset=UTF-8

True, but that is a rather intrusive approach simply for the sake of making
the constructors easier to write; this would allow you to achieve the same
effect transparently. The way I see it, this is sort of an extension of
delegating constructors: delegating to default.

On Wednesday, May 20, 2015 at 7:34:29 AM UTC-6, Igor Baidiuk wrote:
>
> You can achieve this by wrapping into protected/private structure, where
> all your fields are stored. Then adding default copy/move/assignment to
> that struct and doing desired thing in one piece.
>
> On Wednesday, May 20, 2015 at 8:07:47 AM UTC+3, Will Cassella wrote:
>>
>> I ran into this issue the other day when writing a large class that needs
>> additional book-keeping logic when being moved or copied. All fields could
>> be copied/moved normally (just as the default versions would do), there was
>> just a few extra function calls that needed to be made to sort things out.
>> Whenever I added/removed fields in the class, I would have to reflect those
>> changes in all my constructors, which became both bug-prone and a hassle.
>> If there was a simple way to create a default move/copy constructor with
>> some additional behavior in the body, that would be wonderful. Something
>> along the lines of this:
>>
>> MyType::MyType(const MyType& copy)
>>     : default(copy)
>> {
>>     // Additional logic
>> }
>>
>> The constructor would then copy-construct all fields automatically, as
>> well as running the additional code in the body. A copy-assignment operator
>> could be implemented as such:
>>
>> MyType& operator=(const MyType& copy)
>> {
>>     if (this != &copy)
>>     {
>>         default(copy);
>>         // Addition logic
>>     }
>>     return *this;
>> }
>>
>> Both examples could be easily extended to move construction/assignment as
>> well.
>>
>>

--

---
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

------=_Part_4112_1726272997.1432137137264
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">True, but that is a rather intrusive approach simply for t=
he sake of making the constructors easier to write; this would allow you to=
 achieve the same effect transparently. The way I see it, this is sort of a=
n extension of delegating constructors: delegating to default.<br><br>On We=
dnesday, May 20, 2015 at 7:34:29 AM UTC-6, Igor Baidiuk wrote:<blockquote c=
lass=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px=
 #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">You can achieve this by wr=
apping into protected/private structure, where all your fields are stored. =
Then adding default copy/move/assignment to that struct and doing desired t=
hing in one piece.<br><br>On Wednesday, May 20, 2015 at 8:07:47 AM UTC+3, W=
ill Cassella wrote:<blockquote class=3D"gmail_quote" style=3D"margin:0;marg=
in-left:0.8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir=3D"ltr"=
>I ran into this issue the other day when writing a large class that needs =
additional book-keeping logic when being moved or copied. All fields could =
be copied/moved normally (just as the default versions would do), there was=
 just a few extra function calls that needed to be made to sort things out.=
 Whenever I added/removed fields in the class, I would have to reflect thos=
e changes in all my constructors, which became both bug-prone and a hassle.=
 If there was a simple way to create a default move/copy constructor with s=
ome additional behavior in the body, that would be wonderful. Something alo=
ng the lines of this:<br><br><div style=3D"background-color:rgb(250,250,250=
);border-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wr=
ap:break-word"><code><div><span style=3D"color:#606">MyType</span><span sty=
le=3D"color:#660">::</span><span style=3D"color:#606">MyType</span><span st=
yle=3D"color:#660">(</span><span style=3D"color:#008">const</span><span sty=
le=3D"color:#000"> </span><span style=3D"color:#606">MyType</span><span sty=
le=3D"color:#660">&amp;</span><span style=3D"color:#000"> copy</span><span =
style=3D"color:#660">)</span><span style=3D"color:#000"><br>&nbsp; &nbsp; <=
/span><span style=3D"color:#660">:</span><span style=3D"color:#000"> </span=
><span style=3D"color:#008">default</span><span style=3D"color:#660">(</spa=
n><span style=3D"color:#000">copy</span><span style=3D"color:#660">)</span>=
<span style=3D"color:#000"><br></span><span style=3D"color:#660">{</span><s=
pan style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=3D"color:#800=
">// Additional logic</span><span style=3D"color:#000"><br></span><span sty=
le=3D"color:#660">}</span><span style=3D"color:#000"><br></span></div></cod=
e></div><br>The constructor would then copy-construct all fields automatica=
lly, as well as running the additional code in the body. A copy-assignment =
operator could be implemented as such:<br><br><div style=3D"background-colo=
r:rgb(250,250,250);border-color:rgb(187,187,187);border-style:solid;border-=
width:1px;word-wrap:break-word"><code><div><span style=3D"color:#606">MyTyp=
e</span><span style=3D"color:#660">&amp;</span><span style=3D"color:#000"> =
</span><span style=3D"color:#008">operator</span><span style=3D"color:#660"=
>=3D(</span><span style=3D"color:#008">const</span><span style=3D"color:#00=
0"> </span><span style=3D"color:#606">MyType</span><span style=3D"color:#66=
0">&amp;</span><span style=3D"color:#000"> copy</span><span style=3D"color:=
#660">)</span><span style=3D"color:#000"><br></span><span style=3D"color:#6=
60">{</span><span style=3D"color:#000"><br>&nbsp; &nbsp; </span><span style=
=3D"color:#008">if</span><span style=3D"color:#000"> </span><span style=3D"=
color:#660">(</span><span style=3D"color:#008">this</span><span style=3D"co=
lor:#000"> </span><span style=3D"color:#660">!=3D</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#660">&amp;</span><span style=3D"color=
:#000">copy</span><span style=3D"color:#660">)</span><span style=3D"color:#=
000"><br>&nbsp; &nbsp; </span><span style=3D"color:#660">{</span><span styl=
e=3D"color:#000"><br>&nbsp; &nbsp; &nbsp; &nbsp; </span><span style=3D"colo=
r:#008">default</span><span style=3D"color:#660">(</span><span style=3D"col=
or:#000">copy</span><span style=3D"color:#660">);</span><span style=3D"colo=
r:#000"><br>&nbsp; &nbsp; &nbsp; &nbsp; </span><span style=3D"color:#800">/=
/ Addition logic</span><span style=3D"color:#000"><br>&nbsp; &nbsp; </span>=
<span style=3D"color:#660">}</span><span style=3D"color:#000"><br>&nbsp; &n=
bsp; </span><span style=3D"color:#008">return</span><span style=3D"color:#0=
00"> </span><span style=3D"color:#660">*</span><span style=3D"color:#008">t=
his</span><span style=3D"color:#660">;</span><span style=3D"color:#000"><br=
></span><span style=3D"color:#660">}</span><span style=3D"color:#000"><br><=
/span></div></code></div><br>Both examples could be easily extended to move=
 construction/assignment as well.<br><br></div></blockquote></div></blockqu=
ote></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

------=_Part_4112_1726272997.1432137137264--
------=_Part_4111_1538363890.1432137137264--

.