220 13942 <F4908FC0-1467-42DB-8B33-42326A986C92@gmail.com> article
Path: news.gmane.org!not-for-mail
From: Miro Knejp <miro.knejp@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Reconstruct function idea
Date: Thu, 16 Oct 2014 00:31:34 +0200
Lines: 189
Approved: news@gmane.org
Message-ID: <F4908FC0-1467-42DB-8B33-42326A986C92@gmail.com>
References: <ff612334-ae16-43d1-a650-580d0ce635f2@isocpp.org> <CAGg_6+OH-P=uxCx7YEWdJSRtrRfBYhGXh55eymvyM9Vjr+yKMg@mail.gmail.com> <8a74f08b-7453-4d1a-9f20-bc1d3866617c@isocpp.org> <CAFk2RUZ7w3jpuJU1Hqr2=qfDJQycG0P8az+QwsWCG8mBQWi8Jg@mail.gmail.com> <27e2ae9c-8b1f-4688-9bb3-9caef6bf4f54@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0 (Mac OS X Mail 7.3 \(1878.6\))
Content-Type: multipart/alternative; boundary="Apple-Mail=_E13665DB-02DB-4780-A11D-E6BA09991BF3"
X-Trace: ger.gmane.org 1413412306 865 80.91.229.3 (15 Oct 2014 22:31:46 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Wed, 15 Oct 2014 22:31:46 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBC6ONSXJ54LBBSXL7OQQKGQETNZWPQI@isocpp.org Thu Oct 16 00:31:39 2014
Return-path: <std-proposals+bncBC6ONSXJ54LBBSXL7OQQKGQETNZWPQI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ee0-f71.google.com ([74.125.83.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBC6ONSXJ54LBBSXL7OQQKGQETNZWPQI@isocpp.org>)
	id 1XeX6Z-0002LE-4M
	for gclcip-std-proposals@m.gmane.org; Thu, 16 Oct 2014 00:31:39 +0200
Original-Received: by mail-ee0-f71.google.com with SMTP id d17sf1351407eek.6
        for <gclcip-std-proposals@m.gmane.org>; Wed, 15 Oct 2014 15:31:38 -0700 (PDT)
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:from:message-id:mime-version:subject:date
         :references:to:in-reply-to:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:list-post:list-help:list-archive:list-subscribe
         :list-unsubscribe:content-type;
        bh=mHQO+hg62/DMjfhaDje/cq+N0gEOMn6ykhowjY+BQ+I=;
        b=kR3tQBSdNAjtxgZ2hko8iQ4/KfZuTAy+1KhzKDZGk0DS2KB4g6+67aiUndYJihMSL8
         vj0W+klvll0hq4aApffoQJhdbto5/G1gY6y4p2pORH6T6+ADps189fj8PPcHp2Pc0Gsg
         2R54fyidCoZT/nRKl8DcKzcEpngnlNGZldVfKy5CYJx+hHOT7BVtIX5GKXKc7vO8rkDS
         k7ChK3uCIiNoJVKwWIIG2KsNrZQPAgroxi94kZLrlpA+vIMT/rYjzFuoKkg//8szZ4wR
         DGuwhFKNWcXer8qMGXtj392YLc6v1Of08F9xeFi1Dp6mRiAWAr8ArrHfjAaQrD3lTLIC
         nFpQ==
X-Gm-Message-State: ALoCoQlCAi15HYLAmnW1Yb8PBtpEG6T0GrHT0QY0J+m6mM1AJrUJPGeP7KYWqRBRpyuSL1Flz3Wq
X-Received: by 10.112.95.133 with SMTP id dk5mr1161582lbb.3.1413412298799;
        Wed, 15 Oct 2014 15:31:38 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.180.97.231 with SMTP id ed7ls192768wib.3.gmail; Wed, 15 Oct
 2014 15:31:38 -0700 (PDT)
X-Received: by 10.180.21.230 with SMTP id y6mr651973wie.78.1413412298148;
        Wed, 15 Oct 2014 15:31:38 -0700 (PDT)
Original-Received: from mail-wg0-x22e.google.com (mail-wg0-x22e.google.com [2a00:1450:400c:c00::22e])
        by mx.google.com with ESMTPS id yx2si27256093wjc.11.2014.10.15.15.31.38
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Wed, 15 Oct 2014 15:31:38 -0700 (PDT)
Received-SPF: pass (google.com: domain of miro.knejp@gmail.com designates 2a00:1450:400c:c00::22e as permitted sender) client-ip=2a00:1450:400c:c00::22e;
Original-Received: by mail-wg0-f46.google.com with SMTP id l18so2421679wgh.5
        for <std-proposals@isocpp.org>; Wed, 15 Oct 2014 15:31:38 -0700 (PDT)
X-Received: by 10.180.219.106 with SMTP id pn10mr593744wic.63.1413412297932;
        Wed, 15 Oct 2014 15:31:37 -0700 (PDT)
Original-Received: from [192.168.42.4] (ppp-93-104-177-254.dynamic.mnet-online.de. [93.104.177.254])
        by mx.google.com with ESMTPSA id fx2sm25293099wjb.37.2014.10.15.15.31.36
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Wed, 15 Oct 2014 15:31:37 -0700 (PDT)
In-Reply-To: <27e2ae9c-8b1f-4688-9bb3-9caef6bf4f54@isocpp.org>
X-Mailer: Apple Mail (2.1878.6)
X-Original-Sender: miro.knejp@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of miro.knejp@gmail.com designates 2a00:1450:400c:c00::22e as
 permitted sender) smtp.mail=miro.knejp@gmail.com;       dkim=pass
 header.i=@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=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: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://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>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:13942
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/13942>

--Apple-Mail=_E13665DB-02DB-4780-A11D-E6BA09991BF3
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset=ISO-8859-1


On 16 Oct 2014, at 00:22 , Myriachan <myriachan@gmail.com> wrote:

> On Wednesday, October 15, 2014 3:02:28 PM UTC-7, Ville Voutilainen wrote:
> On 16 October 2014 00:17, Myriachan <myri...@gmail.com> wrote:=20
> >> It's called std::experimental::optional.=20
> > While that is a nice feature, it's not the same as needing to do this t=
o=20
> > types that weren't created as std::optional<T>.=20
>=20
>=20
> Some questions:=20
>=20
> 1) if you can refactor code to take into use this reconstruction=20
> function, do you=20
> think you can't change the same code to start using optional? If so, why?=
=20
>=20
>=20
> The main reason to use something like this is for copy constructors.  Som=
etimes, the most convenient way to implement a copy or move constructor is =
like this:
>=20
> T &T::operator =3D(const T &copy)
> {
>     if (&copy !=3D this)
>     {
>         this->~T();
>         new(this) T(copy);
>     }
>     return *this;
> }
>=20
> ...and equivalently with std::forward for move constructors.

This would never pass my code review. If that constructor throws you're doo=
med. Use copy-and-swap instead, it provides strong exception guarantee and =
won't leave you with a destroyed object. Move constructors are a different =
game though and the compiler-defaulted one should do it's job just fine in =
most cases.

T& T::operator =3D(const T& copy)
{
    if (&copy !=3D this)
    {
        T x{copy};
        swap(*this, x);
    }
    return *this;
}

--=20

---=20
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 e=
mail 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-proposa=
ls/.

--Apple-Mail=_E13665DB-02DB-4780-A11D-E6BA09991BF3
Content-Transfer-Encoding: quoted-printable
Content-Type: text/html; charset=ISO-8859-1

<html><head><meta http-equiv=3D"Content-Type" content=3D"text/html charset=
=3Dus-ascii"></head><body style=3D"word-wrap: break-word; -webkit-nbsp-mode=
: space; -webkit-line-break: after-white-space;"><br><div><div>On 16 Oct 20=
14, at 00:22 , Myriachan &lt;<a href=3D"mailto:myriachan@gmail.com">myriach=
an@gmail.com</a>&gt; wrote:</div><br class=3D"Apple-interchange-newline"><b=
lockquote type=3D"cite"><div dir=3D"ltr">On Wednesday, October 15, 2014 3:0=
2:28 PM UTC-7, Ville Voutilainen wrote:<blockquote class=3D"gmail_quote" st=
yle=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-lef=
t: 1ex;">On 16 October 2014 00:17, Myriachan &lt;<a href=3D"javascript:" ta=
rget=3D"_blank" gdf-obfuscated-mailto=3D"fbazzRHkyrUJ" onmousedown=3D"this.=
href=3D'javascript:';return true;" onclick=3D"this.href=3D'javascript:';ret=
urn true;">myri...@gmail.com</a>&gt; wrote:
<br>&gt;&gt; It's called std::experimental::optional.
<br>&gt; While that is a nice feature, it's not the same as needing to do t=
his to
<br>&gt; types that weren't created as std::optional&lt;T&gt;.
<br>
<br>
<br>Some questions:
<br>
<br>1) if you can refactor code to take into use this reconstruction
<br>function, do you
<br>think you can't change the same code to start using optional? If so, wh=
y?
<br>
<br></blockquote><div><br>The main reason to use something like this is for=
 copy constructors.&nbsp; Sometimes, the most convenient way to implement a=
 copy or move constructor is like this:<br><br><div class=3D"prettyprint" s=
tyle=3D"background-color: rgb(250, 250, 250); border: 1px solid rgb(187, 18=
7, 187); word-wrap: break-word; position: static; z-index: auto;"><code cla=
ss=3D"prettyprint">T <span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">&amp;</span>T<span style=3D"color: #660;" class=3D"styled-by-prettify">=
::</span><span style=3D"color: #008;" class=3D"styled-by-prettify">operator=
</span> <span style=3D"color: #660;" class=3D"styled-by-prettify">=3D(</spa=
n><span style=3D"color: #008;" class=3D"styled-by-prettify">const</span> T =
<span style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span>copy<=
span style=3D"color: #660;" class=3D"styled-by-prettify">)</span><br><span =
style=3D"color: #660;" class=3D"styled-by-prettify">{</span><br>&nbsp; &nbs=
p; <span style=3D"color: #008;" class=3D"styled-by-prettify">if</span> <spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">(&amp;</span>copy <sp=
an style=3D"color: #660;" class=3D"styled-by-prettify">!=3D</span> <span st=
yle=3D"color: #008;" class=3D"styled-by-prettify">this</span><span style=3D=
"color: #660;" class=3D"styled-by-prettify">)</span><br>&nbsp; &nbsp; <span=
 style=3D"color: #660;" class=3D"styled-by-prettify">{</span><br>&nbsp; &nb=
sp; &nbsp; &nbsp; <span style=3D"color: #008;" class=3D"styled-by-prettify"=
>this</span><span style=3D"color: #660;" class=3D"styled-by-prettify">-&gt;=
~</span>T<span style=3D"color: #660;" class=3D"styled-by-prettify">();</spa=
n><br>&nbsp; &nbsp; &nbsp; &nbsp; <span style=3D"color: #008;" class=3D"sty=
led-by-prettify">new</span><span style=3D"color: #660;" class=3D"styled-by-=
prettify">(</span><span style=3D"color: #008;" class=3D"styled-by-prettify"=
>this</span><span style=3D"color: #660;" class=3D"styled-by-prettify">)</sp=
an> T<span style=3D"color: #660;" class=3D"styled-by-prettify">(</span>copy=
<span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><br>&nbs=
p; &nbsp; <span style=3D"color: #660;" class=3D"styled-by-prettify">}</span=
><br>&nbsp; &nbsp; <span style=3D"color: #008;" class=3D"styled-by-prettify=
">return</span> <span style=3D"color: #660;" class=3D"styled-by-prettify">*=
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">this</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><br><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">}</span><br></code></=
div><br>...and equivalently with std::forward for move constructors.<br></d=
iv></div></blockquote><div><br></div>This would never pass my code review. =
If that constructor throws you're doomed. Use copy-and-swap instead, it pro=
vides strong exception guarantee and won't leave you with a destroyed objec=
t. Move constructors are a different game though and the compiler-defaulted=
 one should do it's job just fine in most cases.</div><div><br></div><div><=
div dir=3D"ltr"><div class=3D"prettyprint" style=3D"background-color: rgb(2=
50, 250, 250); border: 1px solid rgb(187, 187, 187); word-wrap: break-word;=
 position: static; z-index: auto;"><code class=3D"prettyprint">T<span class=
=3D"styled-by-prettify" style=3D"color: rgb(102, 102, 0);">&amp;&nbsp;</spa=
n>T<span class=3D"styled-by-prettify" style=3D"color: rgb(102, 102, 0);">::=
</span><span class=3D"styled-by-prettify" style=3D"color: rgb(0, 0, 136);">=
operator</span>&nbsp;<span class=3D"styled-by-prettify" style=3D"color: rgb=
(102, 102, 0);">=3D(</span><span class=3D"styled-by-prettify" style=3D"colo=
r: rgb(0, 0, 136);">const</span>&nbsp;T<span class=3D"styled-by-prettify" s=
tyle=3D"color: rgb(102, 102, 0);">&amp;&nbsp;</span>copy<span class=3D"styl=
ed-by-prettify" style=3D"color: rgb(102, 102, 0);">)</span><br><span class=
=3D"styled-by-prettify" style=3D"color: rgb(102, 102, 0);">{</span><br>&nbs=
p; &nbsp;&nbsp;<span class=3D"styled-by-prettify" style=3D"color: rgb(0, 0,=
 136);">if</span>&nbsp;<span class=3D"styled-by-prettify" style=3D"color: r=
gb(102, 102, 0);">(&amp;</span>copy&nbsp;<span class=3D"styled-by-prettify"=
 style=3D"color: rgb(102, 102, 0);">!=3D</span>&nbsp;<span class=3D"styled-=
by-prettify" style=3D"color: rgb(0, 0, 136);">this</span><span class=3D"sty=
led-by-prettify" style=3D"color: rgb(102, 102, 0);">)</span><br>&nbsp; &nbs=
p;&nbsp;<span class=3D"styled-by-prettify" style=3D"color: rgb(102, 102, 0)=
;">{</span><br>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;T x<span class=3D"styled-by=
-prettify" style=3D"color: rgb(102, 102, 0);">{copy};</span><br>&nbsp; &nbs=
p; &nbsp; &nbsp;&nbsp;<span class=3D"styled-by-prettify" style=3D"color: rg=
b(0, 0, 136);">swap</span><span class=3D"styled-by-prettify" style=3D"color=
: rgb(102, 102, 0);">(*</span><span class=3D"styled-by-prettify" style=3D"c=
olor: rgb(0, 0, 136);">this</span><span class=3D"styled-by-prettify"><font =
color=3D"#666600">,&nbsp;</font></span>x<span class=3D"styled-by-prettify" =
style=3D"color: rgb(102, 102, 0);">);</span><br>&nbsp; &nbsp;&nbsp;<span cl=
ass=3D"styled-by-prettify" style=3D"color: rgb(102, 102, 0);">}</span><br>&=
nbsp; &nbsp;&nbsp;<span class=3D"styled-by-prettify" style=3D"color: rgb(0,=
 0, 136);">return</span>&nbsp;<span class=3D"styled-by-prettify" style=3D"c=
olor: rgb(102, 102, 0);">*</span><span class=3D"styled-by-prettify" style=
=3D"color: rgb(0, 0, 136);">this</span><span class=3D"styled-by-prettify" s=
tyle=3D"color: rgb(102, 102, 0);">;</span><br><span class=3D"styled-by-pret=
tify" style=3D"color: rgb(102, 102, 0);">}</span><br></code></div></div><di=
v></div></div><br></body></html>

<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 />

--Apple-Mail=_E13665DB-02DB-4780-A11D-E6BA09991BF3--

.
