Topic: Add implicit virtual destructors to class using the
Author: Nathan Oliver <nathanoliver60097@gmail.com>
Date: Thu, 21 Mar 2019 14:29:54 -0700 (PDT)
Raw View
------=_Part_478_914678789.1553203794394
Content-Type: multipart/alternative;
boundary="----=_Part_479_1777090644.1553203794394"
------=_Part_479_1777090644.1553203794394
Content-Type: text/plain; charset="UTF-8"
Today I encountered the following code on Stack Overflow
<https://stackoverflow.com/questions/55288708/c-copy-constructor-called-at-return> which
will not compile as it tries to copy the return value.
struct B {};
struct A{
std::unique_ptr<B> x;
virtual ~A() = default;
};
A f() {
A tmp;
return tmp;
}
The problem here is that since the user declared the destructor so it could
be virtual, it removed the implicitly generated move operations. This can
be solved by explicitly defaulting the operations, which adds code you have
to type which introduces more places to create a bug, or you can inherit
from a class that only has a virtual destructor. This is less prone to
bugs but it is still writing boiler plate to add just to get a default
virtual destructor. I think it would be nice to have the ability to mark A
in such a way that the compiler can make its implicitly generated
destructor default. Something along the lines of
virtual struct A {
std::unique_ptr<B> x;
};
Would be nice to be able to do to tell the compiler to generate a virtual
destructor and allows you to follow the rule of zero.
--
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/9a1898a8-77dc-42a3-8b62-67f49dcaaac6%40isocpp.org.
------=_Part_479_1777090644.1553203794394
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr">Today I encountered the following code on <a href=3D"https=
://stackoverflow.com/questions/55288708/c-copy-constructor-called-at-return=
">Stack Overflow</a>=C2=A0which will not compile as it tries to copy the re=
turn value.<div><br></div><div><div class=3D"prettyprint" style=3D"backgrou=
nd-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); border-styl=
e: solid; border-width: 1px; overflow-wrap: break-word;"><code class=3D"pre=
ttyprint"><div class=3D"subprettyprint"><pre title=3D"double-click to selec=
t all" class=3D"lang-cpp prettyprint prettyprinted" style=3D"padding: 12px =
8px; font-variant-numeric: inherit; font-variant-east-asian: inherit; font-=
stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monac=
o, "Lucida Console", "Liberation Mono", "DejaVu Sa=
ns Mono", "Bitstream Vera Sans Mono", "Courier New"=
;, monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; wi=
dth: auto; max-height: 600px; overflow: auto; background-color: rgb(239, 24=
0, 241); border-radius: 3px; color: rgb(57, 51, 24); overflow-wrap: normal;=
"><pre title=3D"double-click to select all" class=3D"lang-cpp prettyprint p=
rettyprinted" style=3D"padding: 12px 8px; font-variant-numeric: inherit; fo=
nt-variant-east-asian: inherit; font-stretch: inherit; line-height: inherit=
; font-family: Consolas, Menlo, Monaco, "Lucida Console", "L=
iberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sa=
ns Mono", "Courier New", monospace, sans-serif; vertical-ali=
gn: baseline; box-sizing: inherit; width: auto; max-height: 600px; overflow=
: auto; border-radius: 3px; overflow-wrap: normal;"><code style=3D"font-sty=
le: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inh=
erit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Luc=
ida Console", "Liberation Mono", "DejaVu Sans Mono"=
;, "Bitstream Vera Sans Mono", "Courier New", monospace=
, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: i=
nherit;"><span style=3D"color: #008;" class=3D"styled-by-prettify">struct</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"> B </span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">{};</span><span sty=
le=3D"color: #000;" class=3D"styled-by-prettify"><br><br></span><span style=
=3D"color: #008;" class=3D"styled-by-prettify">struct</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"> A</span><span style=3D"color: #=
660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" cla=
ss=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 std</span><span style=3D"color:=
#660;" class=3D"styled-by-prettify">::</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify">unique_ptr</span><span style=3D"color: #660;" =
class=3D"styled-by-prettify"><</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify">B</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">></span><span style=3D"color: #000;" class=3D"styled-by-p=
rettify"> x</span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=C2=
=A0 =C2=A0 </span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>virtual</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> <=
/span><span style=3D"color: #660;" class=3D"styled-by-prettify">~</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify">A</span><span style=
=3D"color: #660;" class=3D"styled-by-prettify">()</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;"=
class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"style=
d-by-prettify">default</span><span style=3D"color: #660;" class=3D"styled-b=
y-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-prettif=
y"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">};<=
/span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br><br>A f=
</span><span style=3D"color: #660;" class=3D"styled-by-prettify">()</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 A tmp</span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 </span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">return</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> tmp</span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" cla=
ss=3D"styled-by-prettify">}</span></code></pre></pre></div></code></div><br=
>The problem here is that since the user declared the destructor so it coul=
d be virtual, it removed the implicitly generated move operations.=C2=A0 Th=
is can be solved by explicitly defaulting the operations, which adds code y=
ou have to type which introduces more places to create a bug, or you can in=
herit from a class that only has a virtual destructor.=C2=A0 This is less p=
rone to bugs but it is still writing boiler plate to add just to get a defa=
ult virtual destructor.=C2=A0 I think it would be nice to have the ability =
to mark A in such a way that the compiler can make its implicitly generated=
destructor default.=C2=A0 Something along the lines of<br></div><div><br><=
/div><div><div class=3D"prettyprint" style=3D"background-color: rgb(250, 25=
0, 250); border-color: rgb(187, 187, 187); border-style: solid; border-widt=
h: 1px; overflow-wrap: break-word;"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><pre title=3D"double-click to select all" class=3D"lang=
-cpp prettyprint prettyprinted" style=3D"padding: 12px 8px; font-variant-nu=
meric: inherit; font-variant-east-asian: inherit; font-stretch: inherit; li=
ne-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Cons=
ole", "Liberation Mono", "DejaVu Sans Mono", "=
;Bitstream Vera Sans Mono", "Courier New", monospace, sans-s=
erif; vertical-align: baseline; box-sizing: inherit; width: auto; max-heigh=
t: 600px; overflow: auto; background-color: rgb(239, 240, 241); border-radi=
us: 3px; color: rgb(57, 51, 24); overflow-wrap: normal;"><code style=3D"fon=
t-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch=
: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, &quo=
t;Lucida Console", "Liberation Mono", "DejaVu Sans Mono=
", "Bitstream Vera Sans Mono", "Courier New", mono=
space, sans-serif; vertical-align: baseline; box-sizing: inherit; white-spa=
ce: inherit;"><span style=3D"color: #008;" class=3D"styled-by-prettify">vir=
tual</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">struct</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> A </span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">{</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"><br>=C2=A0 =C2=A0 std</span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">::</span><span style=3D"=
color: #000;" class=3D"styled-by-prettify">unique_ptr</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify"><</span><span style=3D"color:=
#000;" class=3D"styled-by-prettify">B</span><span style=3D"color: #660;" c=
lass=3D"styled-by-prettify">></span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> x</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"><br></span><span style=3D"color: #660;" class=3D"styled-by-prettify"=
>};</span></code></pre></div></code></div><br>Would be nice to be able to d=
o to tell the compiler to generate a virtual destructor and allows you to f=
ollow the rule of zero.</div></div>
<p></p>
-- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals" 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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/9a1898a8-77dc-42a3-8b62-67f49dcaaac6%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/9a1898a8-77dc-42a3-8b62-67f49dcaaac6=
%40isocpp.org</a>.<br />
------=_Part_479_1777090644.1553203794394--
------=_Part_478_914678789.1553203794394--
.