220 18859 <07A8C4E9-69D7-4EE2-AADB-7179FB93BEA1@gmail.com> article
Path: news.gmane.org!not-for-mail
From: Vitali Lovich <vlovich@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Compressing std::optional
Date: Sun, 28 Jun 2015 09:19:51 -0700
Lines: 259
Approved: news@gmane.org
Message-ID: <07A8C4E9-69D7-4EE2-AADB-7179FB93BEA1@gmail.com>
References: <4359ebfb-5e20-42c1-84a0-16ef6201db33@isocpp.org> <33303ec4-d4c3-4080-a7bb-346496e38114@isocpp.org> <CA+jCFLu=mau8v+XJxAg7eUwk=uLXdXLhhGTV9KsOxT=CjtzqfQ@mail.gmail.com> <CAOHCbiv9J9S=U02d=P3jrDN8WD1dLpoWsfz3Jmot7LV7JHQAVw@mail.gmail.com> <749546C6-8165-4775-A3E2-319F6EA84EE5@gmail.com> <8fc1d9f9-41a8-431c-a89d-448bea95a5db@isocpp.org> <D5EEF02F-4CA7-49C0-A40E-E3DCA4EDB2C5@mac.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0 (1.0)
Content-Type: multipart/alternative;
	boundary=Apple-Mail-1951A2E4-13C5-493F-9A54-81594CA59B73
Content-Transfer-Encoding: 7bit
X-Trace: ger.gmane.org 1435508405 2843 80.91.229.3 (28 Jun 2015 16:20:05 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Sun, 28 Jun 2015 16:20:05 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCFIZX46Q4HRBLV5YCWAKGQERZU4JII@isocpp.org Sun Jun 28 18:20:00 2015
Return-path: <std-proposals+bncBCFIZX46Q4HRBLV5YCWAKGQERZU4JII@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-oi0-f70.google.com ([209.85.218.70])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCFIZX46Q4HRBLV5YCWAKGQERZU4JII@isocpp.org>)
	id 1Z9FJH-0008Bl-Rc
	for gclcip-std-proposals@m.gmane.org; Sun, 28 Jun 2015 18:20:00 +0200
Original-Received: by oibr82 with SMTP id r82sf251776795oib.1
        for <gclcip-std-proposals@m.gmane.org>; Sun, 28 Jun 2015 09:19:58 -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:content-type:content-transfer-encoding
         :mime-version:subject:message-id:date:references:in-reply-to:to
         :x-original-sender:x-original-authentication-results:reply-to
         :precedence:mailing-list:list-id:x-spam-checked-in-group:list-post
         :list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=2cDPf68e0pvWQEjT4CF+KqUhejKWCAMbR+Uo2vDh3tE=;
        b=WNj/I/c1OZrsNP0FvMoe9LBzkt+ceBdCaLywrf4o589QF2v+3Hk9lCov+o5+9IEojC
         U266Fsg9R9VySUax9z+JUWXZBw7lLIkQfi/xHcC1EiEITIq9cppI9aRjw158kjmtkAv4
         dvWCVo5Vw4g40nDVWeOjQ88HH9nUpkK91HeyfTUDtSdGfQdjvucf35I/q3ZWDtXoAGpp
         C9hWRkhAnK+Wvrbhh4iFz/IjeScaIM9bYakC4h1RahVF8MQKgVLdcIfN/tgAAXMm/HF7
         jCLIzfK3dDjmfY+SQu+4bq7IUSdVaVWDkt6dTXc6NXyOvel/h2CY044yRlHQ2b4r54B/ 
X-Gm-Message-State: ALoCoQmjT8kMvv8+PEYnN7zGIuziwIiOATfy1ICaXONbWlczDW+V1mdvH4FsFfKgW90RTN8nw3Iv
X-Received: by 10.43.160.202 with SMTP id md10mr15759385icc.19.1435508398826;
        Sun, 28 Jun 2015 09:19:58 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.107.16.81 with SMTP id y78ls1517694ioi.57.gmail; Sun, 28 Jun
 2015 09:19:58 -0700 (PDT)
X-Received: by 10.68.57.206 with SMTP id k14mr3374227pbq.97.1435508398258;
        Sun, 28 Jun 2015 09:19:58 -0700 (PDT)
Original-Received: from mail-pa0-x230.google.com (mail-pa0-x230.google.com. [2607:f8b0:400e:c03::230])
        by mx.google.com with ESMTPS id oe9si60031724pbc.247.2015.06.28.09.19.58
        for <std-proposals@isocpp.org>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Sun, 28 Jun 2015 09:19:58 -0700 (PDT)
Received-SPF: pass (google.com: domain of vlovich@gmail.com designates 2607:f8b0:400e:c03::230 as permitted sender) client-ip=2607:f8b0:400e:c03::230;
Original-Received: by padev16 with SMTP id ev16so92738638pad.0
        for <std-proposals@isocpp.org>; Sun, 28 Jun 2015 09:19:58 -0700 (PDT)
X-Received: by 10.70.109.199 with SMTP id hu7mr23597958pdb.71.1435508397941;
        Sun, 28 Jun 2015 09:19:57 -0700 (PDT)
Original-Received: from [10.0.0.5] (c-67-180-190-206.hsd1.ca.comcast.net. [67.180.190.206])
        by mx.google.com with ESMTPSA id gc5sm39413499pbb.53.2015.06.28.09.19.56
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Sun, 28 Jun 2015 09:19:56 -0700 (PDT)
In-Reply-To: <D5EEF02F-4CA7-49C0-A40E-E3DCA4EDB2C5@mac.com>
X-Mailer: iPhone Mail (13A290)
X-Original-Sender: vlovich@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of vlovich@gmail.com designates 2607:f8b0:400e:c03::230 as permitted
 sender) smtp.mail=vlovich@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-Spam-Checked-In-Group: 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:18859
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/18859>

--Apple-Mail-1951A2E4-13C5-493F-9A54-81594CA59B73
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable



> On Jun 28, 2015, at 7:38 AM, David Krauss <potswa@mac.com> wrote:
>=20
>=20
>> On 2015=E2=80=9306=E2=80=9328, at 3:47 AM, vlovich@gmail.com wrote:
>>=20
>> Hey David, I really like the idea of a basic_optional a lot better & can=
 provide a better foundation for doing something like compressing the optio=
nal state for many optionals into a bit-field (e.g. protobuf) which is one =
the problems left unsolved.
>> I also like the idea of encapsulating the sentinel'ness as you propose (=
e.g. std::optional<never_quiet_nan<double>>) instead of having a separate t=
raits.  Can you elaborate a bit on how this could be accomplished?  Right n=
ow optional has a boolean
>> field.  My original design moved that boolean field into the policy/trai=
ts & used the empty base-class optimization to get rid of it when unnecessa=
ry.  In the std::optional<never_quiet_nan<double>> example, would never_qui=
et_nan have a static constexpr member
>> boolean that said "I understand how to read/write my state from the valu=
e"?  Or would there be an ADL traits class that parametrized this?
>=20
> I=E2=80=99m thinking like this:
>=20
> // Thin wrapper:
>=20
> template< typename float_t >
> struct never_quiet_nan {
>     float_t value =3D 0;
>=20
>     operator float_t & () { return value; }
>     operator float_t const & () const { return value; }
> };
>=20
> // ADL object representation functions:
>=20
> template< typename float_t >
> void invalidate_representation( never_quiet_nan< float_t > * invalid_ptr =
)
>     { * reinterpret_cast< float_t * >( invalid_ptr ) =3D std::numeric_lim=
its< float_t >::quiet_NaN; }
>=20
> template< typename float_t >
> bool is_valid_representation( never_quiet_nan< float_t > const * question=
able_ptr )
>     { return std::isnan( * reinterpret_cast< float_t const * >( questiona=
ble_ptr ) ); }
Hmmm... And how would optional decide what state it needs to store?

>> The std::vector & polymorphism cases I think are less compelling for as-=
if as I can't imagine a use-case where you would store either in optional. =
 std::vector has a natural empty state & using optional<vector<X>> seems mo=
re likely to be a poor design
>> than anything else.=20
>=20
> One use-case of std::optional is for output parameters. Those are already=
 an anti-pattern per se, but still I think a blanket statement that optiona=
l<vector> should never happen is too strong.
>=20
> Under as-if, it=E2=80=99s a question of how many priorities a standard li=
brary can implement before they need to freeze their ABI.
>=20
>> Polymorphic classes tend to be prone to slicing when used as a value-typ=
e.  optional for enum classes (enums too?) would be possible if there were =
reflection so that you could query for an out-of-range value to use as a se=
ntinel.
>=20
> Just ask the user for invalidate_representation and is_valid_representati=
on. Enumerations aren=E2=80=99t closed so reflection can=E2=80=99t really d=
iscover invalid values.
>=20
>> As you say, an implementation would be free to optimize these if it felt=
 important under the as-if rule so there would be no need to specify it in =
the proposal.
>>=20
>> I was hoping to get to writing up the proposal this weekend but it looks=
 like probably it will end up next weekend.  If you have any more thoughts =
on the matter feel free to email me directly.
>=20
> My hands are full=E2=80=A6 I=E2=80=99m procrastinating here already :P .
>=20
> Good luck!
>=20
> --=20
>=20
> ---=20
> You received this message because you are subscribed to a topic in the Go=
ogle Groups "ISO C++ Standard - Future Proposals" group.
> To unsubscribe from this topic, visit https://groups.google.com/a/isocpp.=
org/d/topic/std-proposals/46J1onhWJ-s/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to std-p=
roposals+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-propo=
sals/.

--=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-1951A2E4-13C5-493F-9A54-81594CA59B73
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html><head><meta http-equiv=3D"content-type" content=3D"text/html; charset=
=3Dutf-8"></head><body dir=3D"auto"><div></div><div><br></div><div><br>On J=
un 28, 2015, at 7:38 AM, David Krauss &lt;<a href=3D"mailto:potswa@mac.com"=
>potswa@mac.com</a>&gt; wrote:<br><br></div><blockquote type=3D"cite"><div>=
<meta http-equiv=3D"Content-Type" content=3D"text/html charset=3Dutf-8"><br=
 class=3D""><div><blockquote type=3D"cite" class=3D""><div class=3D"">On 20=
15=E2=80=9306=E2=80=9328, at 3:47 AM, <a href=3D"mailto:vlovich@gmail.com" =
class=3D"">vlovich@gmail.com</a> wrote:</div><br class=3D"Apple-interchange=
-newline"><div class=3D""><div dir=3D"ltr" style=3D"font-family: Helvetica;=
 font-size: 12px; font-style: normal; font-variant: normal; font-weight: no=
rmal; letter-spacing: normal; line-height: normal; orphans: auto; text-alig=
n: start; text-indent: 0px; text-transform: none; white-space: normal; wido=
ws: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=3D"">He=
y David, I really like the idea of a basic_optional a lot better &amp; can =
provide a better foundation for doing something like compressing the option=
al state for many optionals into a bit-field (e.g. protobuf) which is one t=
he problems left unsolved.<br class=3D"">I also like the idea of encapsulat=
ing the sentinel'ness as you propose (e.g. std::optional&lt;never_quiet_nan=
&lt;double&gt;&gt;) instead of having a separate traits.&nbsp; Can you elab=
orate a bit on how this could be accomplished?&nbsp; Right now optional has=
 a boolean<br class=3D"">field.&nbsp; My original design moved that boolean=
 field into the policy/traits &amp; used the empty base-class optimization =
to get rid of it when unnecessary.&nbsp; In the std::optional&lt;never_quie=
t_nan&lt;double&gt;&gt; example, would never_quiet_nan have a static conste=
xpr member<br class=3D"">boolean that said "I understand how to read/write =
my state from the value"?&nbsp; Or would there be an ADL traits class that =
parametrized this?<br class=3D""></div></div></blockquote><div><br class=3D=
""></div><div>I=E2=80=99m thinking like this:</div><div><br class=3D""></di=
v><div><font face=3D"Courier" class=3D"">// Thin wrapper:</font></div><div>=
<font face=3D"Courier" class=3D""><br class=3D""></font></div><div><span st=
yle=3D"font-family: Courier;" class=3D"">template&lt; typename float_t &gt;=
</span></div><div><font face=3D"Courier" class=3D"">struct never_quiet_nan =
{</font></div><div><font face=3D"Courier" class=3D"">&nbsp; &nbsp; float_t =
value =3D 0;</font></div><div><font face=3D"Courier" class=3D""><br class=
=3D""></font></div><div><font face=3D"Courier" class=3D"">&nbsp; &nbsp; ope=
rator float_t &amp; () { return value; }</font></div><font face=3D"Courier"=
 class=3D"">&nbsp; &nbsp; operator float_t const &amp; () const { return va=
lue; }</font></div><div class=3D""><font face=3D"Courier" class=3D"">};</fo=
nt></div><div class=3D""><font face=3D"Courier" class=3D""><br class=3D""><=
/font></div><div class=3D""><font face=3D"Courier" class=3D"">// ADL object=
 representation functions:</font></div><div class=3D""><font face=3D"Courie=
r" class=3D""><br class=3D""></font></div><div><font face=3D"Courier" class=
=3D"">template&lt; typename float_t &gt;</font></div><div class=3D""><font =
face=3D"Courier" class=3D"">void invalidate_representation(&nbsp;</font><sp=
an style=3D"font-family: Courier;" class=3D"">never_quiet_nan&lt; float_t &=
gt;</span><font face=3D"Courier" class=3D"">&nbsp;* invalid_ptr )<br class=
=3D"">&nbsp; &nbsp; { * reinterpret_cast&lt;&nbsp;</font><span style=3D"fon=
t-family: Courier;" class=3D"">float_t</span><font face=3D"Courier" class=
=3D"">&nbsp;* &gt;(&nbsp;invalid_ptr&nbsp;) =3D std::numeric_limits&lt; flo=
at_t &gt;::quiet_NaN; }<br class=3D""><br class=3D""></font><div><font face=
=3D"Courier" class=3D"">template&lt; typename float_t &gt;</font></div><fon=
t face=3D"Courier" class=3D"">bool is_valid_representation(&nbsp;</font><sp=
an style=3D"font-family: Courier;" class=3D"">never_quiet_nan&lt; float_t &=
gt;</span><font face=3D"Courier" class=3D"">&nbsp;const * questionable_ptr =
)<br class=3D"">&nbsp; &nbsp; { return std::isnan( * reinterpret_cast&lt;&n=
bsp;</font><span style=3D"font-family: Courier;" class=3D"">float_t</span><=
font face=3D"Courier" class=3D"">&nbsp;const * &gt;(&nbsp;questionable_ptr&=
nbsp;) ); }</font><br class=3D""></div></div></blockquote>Hmmm... And how w=
ould optional decide what state it needs to store?<div><br><blockquote type=
=3D"cite"><div><div class=3D""><blockquote type=3D"cite" class=3D""><div cl=
ass=3D""><div dir=3D"ltr" style=3D"font-family: Helvetica; font-size: 12px;=
 font-style: normal; font-variant: normal; font-weight: normal; letter-spac=
ing: normal; line-height: normal; orphans: auto; text-align: start; text-in=
dent: 0px; text-transform: none; white-space: normal; widows: auto; word-sp=
acing: 0px; -webkit-text-stroke-width: 0px;" class=3D"">The std::vector &am=
p; polymorphism cases I think are less compelling for as-if as I can't imag=
ine a use-case where you would store either in optional.&nbsp; std::vector =
has a natural empty state &amp; using optional&lt;vector&lt;X&gt;&gt; seems=
 more likely to be a poor design<br class=3D"">than anything else.&nbsp; </=
div></div></blockquote><div class=3D""><br class=3D""></div><div class=3D""=
>One use-case of <font face=3D"Courier" class=3D"">std::optional</font> is =
for output parameters. Those are already an anti-pattern per se, but still =
I think a blanket statement that <font face=3D"Courier" class=3D"">optional=
&lt;vector&gt;</font> should never happen is too strong.</div><div class=3D=
""><br class=3D""></div><div class=3D"">Under as-if, it=E2=80=99s a questio=
n of how many priorities a standard library can implement before they need =
to freeze their ABI.</div><br class=3D""><blockquote type=3D"cite" class=3D=
""><div class=3D""><div dir=3D"ltr" style=3D"font-family: Helvetica; font-s=
ize: 12px; font-style: normal; font-variant: normal; font-weight: normal; l=
etter-spacing: normal; line-height: normal; orphans: auto; text-align: star=
t; text-indent: 0px; text-transform: none; white-space: normal; widows: aut=
o; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class=3D"">Polymorph=
ic classes tend to be prone to slicing when used as a value-type.&nbsp; opt=
ional for enum classes (enums too?) would be possible if there were reflect=
ion so that you could query for an out-of-range value to use as a sentinel.=
<br class=3D""></div></div></blockquote><div class=3D""><br class=3D""></di=
v><div class=3D"">Just ask the user for <font face=3D"Courier" class=3D"">i=
nvalidate_representation</font> and <font face=3D"Courier" class=3D"">is_va=
lid_representation</font>. Enumerations aren=E2=80=99t closed so reflection=
 can=E2=80=99t really discover invalid values.</div><br class=3D""><blockqu=
ote type=3D"cite" class=3D""><div class=3D""><div dir=3D"ltr" style=3D"font=
-family: Helvetica; font-size: 12px; font-style: normal; font-variant: norm=
al; font-weight: normal; letter-spacing: normal; line-height: normal; orpha=
ns: auto; text-align: start; text-indent: 0px; text-transform: none; white-=
space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: =
0px;" class=3D"">As you say, an implementation would be free to optimize th=
ese if it felt important under the as-if rule so there would be no need to =
specify it in the proposal.<br class=3D""><br class=3D"">I was hoping to ge=
t to writing up the proposal this weekend but it looks like probably it wil=
l end up next weekend.&nbsp; If you have any more thoughts on the matter fe=
el free to email me directly.<br class=3D""></div></div></blockquote></div>=
<br class=3D""><div class=3D"">My hands are full=E2=80=A6 I=E2=80=99m procr=
astinating here already :P .</div><div class=3D""><br class=3D""></div><div=
 class=3D"">Good luck!</div><div class=3D""><br class=3D""></div>

<p></p>

-- <br>
<br>
--- <br>
You received this message because you are subscribed to a topic in the Goog=
le Groups "ISO C++ Standard - Future Proposals" group.<br>
To unsubscribe from this topic, visit <a href=3D"https://groups.google.com/=
a/isocpp.org/d/topic/std-proposals/46J1onhWJ-s/unsubscribe">https://groups.=
google.com/a/isocpp.org/d/topic/std-proposals/46J1onhWJ-s/unsubscribe</a>.<=
br>
To unsubscribe from this group and all its topics, send an email to <a href=
=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposals+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>
</div></blockquote></div></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-1951A2E4-13C5-493F-9A54-81594CA59B73--

.
