220 29558 <1a7a1b18-c001-405e-8f7f-09c048c6ee14@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Instantiation of default destructor
Date: Mon, 28 Nov 2016 07:11:45 -0800 (PST)
Lines: 147
Approved: news@gmane.org
Message-ID: <1a7a1b18-c001-405e-8f7f-09c048c6ee14@isocpp.org>
References: <CAKgx6B+8zzZ131-UZobmUvCgB4YeQ1+wTZadcuT6ieDCofDndg@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_2244_1755540733.1480345905535"
X-Trace: blaine.gmane.org 1480345909 6383 195.159.176.226 (28 Nov 2016 15:11:49 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Mon, 28 Nov 2016 15:11:49 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBMUS6HAQKGQEYIGMUWQ@isocpp.org Mon Nov 28 16:11:44 2016
Return-path: <std-proposals+bncBCEKFTV6ZUMBBMUS6HAQKGQEYIGMUWQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-io0-f200.google.com ([209.85.223.200])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBMUS6HAQKGQEYIGMUWQ@isocpp.org>)
	id 1cBNap-0000zd-Ev
	for gclcip-std-proposals@m.gmane.org; Mon, 28 Nov 2016 16:11:43 +0100
Original-Received: by mail-io0-f200.google.com with SMTP id r94sf247244106ioe.7
        for <gclcip-std-proposals@m.gmane.org>; Mon, 28 Nov 2016 07:11:47 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=sEOvk7xJ6H21+hjmfHgHtADA2JTKPPEvd3FtRgXH1Vk=;
        b=b9Bye1V6Xyx4ILwQOB4+nx7MzJqlaJ0gxP8ECdFIQPSc/Jk6ffgE4SK60drvZmbXLp
         lBZYkZoVP3n/W40qhe6sFjwp7/1s4lxUykS3pCcksT+QaXoS5Ze3/Z+YPSp1rDNhpcLA
         /lNvpkBB8rqXJqEhX8KuI9zBY2wzACqxslngnW7Olv5+vXbqErccdCJyJIUSr0TwXFEV
         ukV7ZZUQelwTuf/xYwhHaBnflFKx/wnWLkc/XZgwFTMyDfyCjY/6FImTL52mgccm9pnK
         5IuNrKunpEnPNG+xYBEnEDgFyKex/j+XSeaOcoPu0Z7f9JG+ZdhTrz95MhiMQB7cNM8S
         sOnQ==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=sEOvk7xJ6H21+hjmfHgHtADA2JTKPPEvd3FtRgXH1Vk=;
        b=kJcDlOEA5HMGUhdZ9q0gDTcsvyX1uHtKpLsDPScGv2NK33HMK7l8zZEVAvoHLV7Fc+
         LkJoF6sZCsuC3BK5cd9PjpgT8Va7uSmCi59IB8hnFk7dNPqM7qFmlpJ6nW2HIm2rQwo4
         XZPykrPwj1EqeepMsH4SpEV6CJTWkY+isKDD5FC5thogW7hW0xEgYJ5ZYicqZ3BBka7M
         D449KXwU10g6s/JWIlLbpCoOISErzuADyz4+5JSKFNylziYnQ91aoMA2YQxnuirjwSO9
         yAeWpjjXfvObnbDD1nu4jO0OgKkBOEd4B06p4Lulw1ehq2mqLhXixBktnXQiEnS9fU+0
         /Eow==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:date:from:to:message-id:in-reply-to:references
         :subject:mime-version:x-original-sender:reply-to:precedence
         :mailing-list:list-id:x-spam-checked-in-group:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=sEOvk7xJ6H21+hjmfHgHtADA2JTKPPEvd3FtRgXH1Vk=;
        b=ZJ9eTM8xHdvb4DrMtRd3+hLZyxOQzewlbTQE5/XK4JHu0o/jyLmBn/qMpDsgYmIDLF
         qR2LGLGr9B8EbFSWZF+aqGBanQOoxi9ntlpFhnr2qpWp22WWxH9mKrtTN/0NIzUDSc9B
         hOQc+HPfPLn9VSZg1teYrbjHHAQhT3OPhjPI9JR461V4TjtK97tqkisioKIkm1Ox1348
         vP9vWhskpV1BiLh+uBid/sNustSbFDYuX5j+McDad+y4WHbOk/cDmjeasv8/tqw0H2/i
         7V2VUtlcOLlGzkluYTKj59I+Pb+d7t2XL3cMimD4TE1uyDsXgp3ZnX6/SvzxJTBjdqLi
         KO+A==
X-Gm-Message-State: AKaTC0152YeEoKNXcqaDdvILCuyIkhPbxR0bUPnNBt6T6rDjQ+9u8TfrSut6o5rbzNKLNA==
X-Received: by 10.36.80.72 with SMTP id m69mr3370198itb.39.1480345906947;
        Mon, 28 Nov 2016 07:11:46 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.27.137 with SMTP id z9ls262558otd.28.gmail; Mon, 28 Nov
 2016 07:11:45 -0800 (PST)
X-Received: by 10.157.56.132 with SMTP id p4mr567331otc.20.1480345905919;
        Mon, 28 Nov 2016 07:11:45 -0800 (PST)
In-Reply-To: <CAKgx6B+8zzZ131-UZobmUvCgB4YeQ1+wTZadcuT6ieDCofDndg@mail.gmail.com>
X-Original-Sender: jmckesson@gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Google-Group-Id: 399137483710
List-Post: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:29558
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/29558>

------=_Part_2244_1755540733.1480345905535
Content-Type: multipart/alternative; 
	boundary="----=_Part_2245_1163896384.1480345905535"

------=_Part_2245_1163896384.1480345905535
Content-Type: text/plain; charset=UTF-8



On Monday, November 28, 2016 at 7:14:55 AM UTC-5, Domen Vrankar wrote:
>
> Hi,
>
> In this video [1] <https://www.youtube.com/watch?v=8AjRD6mU96s> (time 
> 57:40) Jason Jurecka was talking about the need to write empty/defaulted 
> destructor in cpp file if you want to declare a class in header file, use 
> it in std::unique_ptr and include its declaration only in cpp file:
>
> //a.h
> ...
> class T;
>
> struct A
> {
> A(T* ptr_); // intentionally before A() constructor - see the question 
> below
> A();
> std::unique_ptr<T> ptr;
> };
> ...
>
> //a.cpp
> #include "a.h"
> #include "t.h" // declaration of T class
> ...
> A::A() {}
> A::A(T* ptr_) : ptr{ptr_} {}
> ...
>
> This won't work since default destructor is (if I understood the reason 
> why Tomasz Wota's example in the comment worked: [2] 
> <http://melpon.org/wandbox/permlink/k8UpfCT8DlYv1hv3>) implemented at the 
> end of the compilation unit.
>
> For me it's not a big deal to write A::~A() = default; in cpp file but 
> still since we've had that debate I was wondering if it would be feasible 
> to change the wording (don't know how much the standard would have to be 
> changed for that) so that implicit default destructor would be added to the 
> code at the point of first constructor implementation (in the above example 
> at the point of A::A(T* ptr_); implementation in .cpp file) and not at the 
> end of the file where the class was declared?
>
>
You've misunderstood the nature of the problem.

The default destructor will call `unique_ptr::~unique_ptr`. And that 
destructor will call `T::~T()`. The problem is that, unless `T` has been 
*defined*, you cannot call its destructor.

If the compiler defines the destructor for `A`, then it will do so in the 
class definition. That is, in the header. Which means that anyone who 
includes this header who doesn't include the definition of `T` beforehand 
will get a compile error.

By declaring the destructor of `A` in the header, then defaulting the 
destructor in the .cpp, you force the compiler to only generate the class's 
destructor in the .cpp file. Where you have presumably included the 
definition of `T`. And thus, people can use `A` without necessarily having 
the full definition of `T`.

-- 
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/1a7a1b18-c001-405e-8f7f-09c048c6ee14%40isocpp.org.

------=_Part_2245_1163896384.1480345905535
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Monday, November 28, 2016 at 7:14:55 AM UTC-5, =
Domen Vrankar wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;ma=
rgin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr"><div><div>Hi,<br><br></div>In this video <a href=3D"https://www.yo=
utube.com/watch?v=3D8AjRD6mU96s" target=3D"_blank" rel=3D"nofollow" onmouse=
down=3D"this.href=3D&#39;https://www.youtube.com/watch?v\x3d8AjRD6mU96s&#39=
;;return true;" onclick=3D"this.href=3D&#39;https://www.youtube.com/watch?v=
\x3d8AjRD6mU96s&#39;;return true;">[1]</a> (time 57:40) Jason Jurecka was t=
alking about the need to write empty/defaulted destructor in cpp file if yo=
u want to declare a class in header file, use it in std::unique_ptr and inc=
lude its declaration only in cpp file:<br><br></div><div>//a.h<br>...<br></=
div><div>class T;<br><br></div><div>struct A<br></div><div>{<br>A(T* ptr_);=
 // intentionally before A() constructor - see the question below<br></div>=
<div>A();</div><div>std::unique_ptr&lt;T&gt; ptr;<br></div><div>};<br></div=
><div>...<br><br></div><div>//a.cpp<br></div><div>#include &quot;a.h&quot;<=
br></div><div>#include &quot;t.h&quot; // declaration of T class<br></div><=
div>...<br></div><div>A::A() {}<br></div><div>A::A(T* ptr_) : ptr{ptr_} {}<=
br></div><div>...<br><br></div><div>This won&#39;t work since default destr=
uctor is (if I understood the reason why Tomasz Wota&#39;s example in the c=
omment worked: <a href=3D"http://melpon.org/wandbox/permlink/k8UpfCT8DlYv1h=
v3" target=3D"_blank" rel=3D"nofollow" onmousedown=3D"this.href=3D&#39;http=
://www.google.com/url?q\x3dhttp%3A%2F%2Fmelpon.org%2Fwandbox%2Fpermlink%2Fk=
8UpfCT8DlYv1hv3\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGRwN_bv7ckYmx-8tltD=
DU0KqJczw&#39;;return true;" onclick=3D"this.href=3D&#39;http://www.google.=
com/url?q\x3dhttp%3A%2F%2Fmelpon.org%2Fwandbox%2Fpermlink%2Fk8UpfCT8DlYv1hv=
3\x26sa\x3dD\x26sntz\x3d1\x26usg\x3dAFQjCNGRwN_bv7ckYmx-8tltDDU0KqJczw&#39;=
;return true;">[2]</a>) implemented at the end of the compilation unit.<br>=
<br></div><div>For me it&#39;s not a big deal to write A::~A() =3D default;=
 in cpp file but still since we&#39;ve had that debate I was wondering if i=
t would be feasible to change the wording (don&#39;t know how much the stan=
dard would have to be changed for that) so that implicit default destructor=
 would be added to the code at the point of first constructor implementatio=
n (in the above example at the point of A::A(T* ptr_); implementation in .c=
pp file) and not at the end of the file where the class was declared?</div>=
<br></div></blockquote><div><br>You&#39;ve misunderstood the nature of the =
problem.<br><br>The default destructor will call `unique_ptr::~unique_ptr`.=
 And that destructor will call `T::~T()`. The problem is that, unless `T` h=
as been <i>defined</i>, you cannot call its destructor.<br><br>If the compi=
ler defines the destructor for `A`, then it will do so in the class definit=
ion. That is, in the header. Which means that anyone who includes this head=
er who doesn&#39;t include the definition of `T` beforehand will get a comp=
ile error.<br><br>By declaring the destructor of `A` in the header, then de=
faulting the destructor in the .cpp, you force the compiler to only generat=
e the class&#39;s destructor in the .cpp file. Where you have presumably in=
cluded the definition of `T`. And thus, people can use `A` without necessar=
ily having the full definition of `T`.<br></div></div>

<p></p>

-- <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 />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/1a7a1b18-c001-405e-8f7f-09c048c6ee14%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/1a7a1b18-c001-405e-8f7f-09c048c6ee14=
%40isocpp.org</a>.<br />

------=_Part_2245_1163896384.1480345905535--

------=_Part_2244_1755540733.1480345905535--

.
