220 8726 <6c82c4ae-c144-443a-81b2-ea1a6b645601@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Paul Tessier <phernost@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: string_view::is_null()
Date: Sun, 19 Jan 2014 20:07:45 -0800 (PST)
Lines: 249
Approved: news@gmane.org
Message-ID: <6c82c4ae-c144-443a-81b2-ea1a6b645601@isocpp.org>
References: <CAGNvRgDH=X2h4K_u3zOu7gUNuQbAcNHGNh=HfCRO9qG1mmiG1w@mail.gmail.com>
 <CANh-dXnytwWwS7E_BGA-xK8E-zw788TJKzM=HJdC-9JbF7+gTQ@mail.gmail.com>
 <104254A6-C69A-4571-8455-89D7E466D493@gmail.com>
 <CANh-dXngzdnwZZnLRxd=AXQcqLaQky9Cpdw6pkbH8tFqRyZavw@mail.gmail.com>
 <6a95bf9c-55c9-4ae2-966f-d1b1f4df6a37@isocpp.org>
 <52D9E336.1060801@knejp.de>
 <1B534FD7-B385-4A7E-A718-3C73F0AB61DC@gmail.com>
 <11f13f8f-1270-427c-8942-94027a181ff4@isocpp.org>
 <20140119174431.GA16875@faust.lysator.liu.se>
 <CAPOJ94Nt8tinr8hGeR-=rWZS0a529kY38NU-yo6BYM6kVo7czg@mail.gmail.com>
 <20140119192238.GA19497@faust.lysator.liu.se>
 <60842af6-5ec4-4364-b2d5-a0be9d578bd6@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_1627_22142613.1390190865793"
X-Trace: ger.gmane.org 1390190862 14487 80.91.229.3 (20 Jan 2014 04:07:42 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 20 Jan 2014 04:07:42 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDDYTQX56INBBEWC6KLAKGQEFGIGDWA@isocpp.org Mon Jan 20 05:07:50 2014
Return-path: <std-proposals+bncBDDYTQX56INBBEWC6KLAKGQEFGIGDWA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yk0-f197.google.com ([209.85.160.197])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDDYTQX56INBBEWC6KLAKGQEFGIGDWA@isocpp.org>)
	id 1W569M-0008Ie-6x
	for gclcip-std-proposals@m.gmane.org; Mon, 20 Jan 2014 05:07:48 +0100
Original-Received: by mail-yk0-f197.google.com with SMTP id 20sf5726118yks.0
        for <gclcip-std-proposals@m.gmane.org>; Sun, 19 Jan 2014 20:07:47 -0800 (PST)
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
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe
         :content-type;
        bh=hkYBN0C51xaltXDeY/RIQheu5jOTgM754B5zBH2UCC0=;
        b=K1bk5534Q8zzOMB0yTg5wrsZ8ZuIBAmmFgP32Ihv3XTGe7dKUXSgT8G9yRGEnwoI1b
         GFa3dlrc6J33o8aRDtBkxCgszXxw+HktLUFzfZrCgk/a7ofFu1g/S36otNNPzM8MyyIz
         Di4lnm5sruA2mfBMGpVOFgXAVElIXxnIN1W1sKrXztZBXZXosR684fgL12NlYBv89Ejs
         u3s9DoPJ9rDZ/+KF7hMlBLCeqxZxs5q+NRXX+SW/6YPVUt0Ru0LK1CvI03rA65lyvZvI
         8y/ClLiaSJEq4Ox/1T0slerIdCP3eaC6FYq4ktAU33xvdadF5S3sRQaK/xb+zeDnH9C/
         0H1Q==
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:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe:content-type;
        bh=hkYBN0C51xaltXDeY/RIQheu5jOTgM754B5zBH2UCC0=;
        b=cFrQAIemWtmC976nj4rNTwOko29oNstZ/w5GWkzxxqhU7Et3ENQ1uAZl2LveuZC6XH
         Pqm3weUFAsRXOLzQE61Y3gYuFSsluqT2fTHatbSGVNeIobaHb9aJthtyV12QlIj11yUd
         sf38IoE81E8BR6+HIFb58FugF3D52TPSXnsiJb3F4eJ7VfPLah0Nj1kNkEH/ev93zBRo
         wOSuGu7x1eN3yac726aKSoQ5WrZTDjQQDStsv46aEYtYYATl5WxNznHeNuBLyChOKkh7
         bRWaJcJcv1iP9lIpGtNDeZD9ibuDrtKpX8/QXWtmFWx2JJ6yRoJq8BDReFrjREK53OH4
         n0dw==
X-Gm-Message-State: ALoCoQlkw9o/uaJp2H40EPeVUE+bMPcCBkOsw5OQgON3UBAhWa1djLAljhLtPli4j5gPN0cDg4Ce
X-Received: by 10.224.121.70 with SMTP id g6mr2223838qar.1.1390190867151;
        Sun, 19 Jan 2014 20:07:47 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.48.165 with SMTP id m5ls27778qen.52.gmail; Sun, 19 Jan 2014
 20:07:46 -0800 (PST)
X-Received: by 10.140.48.8 with SMTP id n8mr515qga.36.1390190866578;
        Sun, 19 Jan 2014 20:07:46 -0800 (PST)
In-Reply-To: <60842af6-5ec4-4364-b2d5-a0be9d578bd6@isocpp.org>
X-Original-Sender: phernost@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: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:8726
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/8726>

------=_Part_1627_22142613.1390190865793
Content-Type: text/plain; charset=UTF-8



On Sunday, January 19, 2014 8:54:32 PM UTC-5, Peter Bigot wrote:
>
>
>
> On Sunday, January 19, 2014 1:22:40 PM UTC-6, Magnus Fromreide wrote:
>>
>> On Sun, Jan 19, 2014 at 12:31:19PM -0600, Peter Bigot wrote: 
>> > On Sun, Jan 19, 2014 at 11:44 AM, Magnus Fromreide <
>> ma...@lysator.liu.se>wrote: 
>> > 
>> > > On Sun, Jan 19, 2014 at 07:55:32AM -0800, Peter Bigot wrote: 
>> > > > On Friday, January 17, 2014 10:19:07 PM UTC-6, Marshall wrote: 
>> > > > > 
>> > > 
>> > > I have thought in a similar direction, but the problem is if you have 
>> two 
>> > > string_view's, s1 and s2. Assume that s1 i empty, does the statement 
>> > > s2 = s1; imply that s2.data() == s1.data()? 
>> > > 
>> > > Then what happens if s1 is deallocated and it's memory is returned to 
>> the 
>> > > system, won't s2.m_ptr then hold an illegal pointer value, one of 
>> those 
>> > > where even loading it could trigger a hardware trap on some 
>> architectures. 
>> > > 
>> > 
>> > In my approach I detect in the assignment operator and copy constructor 
>> > whether the RHS is default-constructed, and if so invoke clear() on the 
>> > LHS/new instance.  So in all cases either the reference is to a 
>> sequence 
>> > outside the instance, or it's an empty default-constructed (cleared) 
>> > instance where data() is a non-dereferenceable pointer to an internal 
>> charT 
>> > object: no cross-object pointers. 
>> > 
>> > Now that you point it out, this does result in behavior non-conformant 
>> with 
>> > the current draft specification for the copy constructor and operator= 
>> > which are specified as =default. 
>> > 
>> > On further reflection it'd better to store a null pointer in m_ptr to 
>> > represent a default-constructed value but to return &m_nul from data() 
>> when 
>> > m_ptr is null.  Then the default implementations are retained.  This 
>> still 
>> > means that data() will be always be non-null, but sv1.data() will not 
>> > compare equal to sv2.data() if one or both of the instances have been 
>> > cleared/default-constructed.  (sv1 == sv2 will still hold, of course.) 
>> > 
>> > Does that address the objection? 
>>
>> The check at copy time seems to solve the problem. 
>>
>> The use of null as a flag value seems to interfere with the "allow null 
>> string_views" 
>
>
> Yes.  I am not in favor of string_view supporting null values as well as 
> empty values; absence of a value is a distinct concept that should be 
> treated independently as with std::optional.  I think string_view should 
> support the intersection, not the union, of the information provided by its 
> underlying concepts (a) std::string and (b) a pair (const charT * ptr, 
> size_t len).  Only the latter is capable of expressing absence-of-a-value 
> as distinct from empty-string.  (In fact it expresses multiple 
> absence-of-a-value as you could wish to encode information in a non-zero 
> size() paired with a null data().  Madness that way lies.  Just say no to 
> null data().)
>

Why would ( nullptr, 3 ) be considered an empty view?  It's garbage in 
almost all use cases but, so is ( (char*)1, 3 ).  Defending against one 
single bad pointer and ignoring all the rest, solves one out of a myriad of 
similar problems.  A null view ( null, 0 ) is no different than an empty 
view ( (char*)rand(), 0 ).  Although, is_null seems pointless if it can be 
guaranteed that the following be true.

For any empty string_view N constructed as ( string_view N{ P, 0 } ), that 
N.data(), N.begin(), N.end(), etc. all return P.

Currently P is banned from being null.  Which seems to solve one corner 
case and, creates problems for other, otherwise logical, use cases.
 

>
> I do think that string_view is more a reference type than a collection 
> type (thought it is both), so there may be an argument in support of being 
> able to detect whether an instance holds a valid reference (i.e. does not 
> have a default-constructed value, including a value equivalent to 
> default-construction due to invocation of clear()).  If this is desirable, 
> it can be detected easily by adding a member function has_reference() that 
> returns !!m_ptr.
>  
>

All references can become dangling by one way or another.  What you 
described is a smart pointer with a string interface, which if that's what 
you want, I can argue against.  It may solve many use cases but, seems 
overkill at least considering the current stated goal of string_view.
 

> and "have an optimized optional<string_view>" proposals 
>> as both allow null's for other purposes. 
>>
>
> I can't speak to that question.
>
> Peter
>

-- 

--- 
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_1627_22142613.1390190865793
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><br>On Sunday, January 19, 2014 8:54:32 PM UTC-5, Pete=
r Bigot wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-l=
eft: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"=
><br><br>On Sunday, January 19, 2014 1:22:40 PM UTC-6, Magnus Fromreide wro=
te:<blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8ex;bo=
rder-left:1px #ccc solid;padding-left:1ex">On Sun, Jan 19, 2014 at 12:31:19=
PM -0600, Peter Bigot wrote:
<br>&gt; On Sun, Jan 19, 2014 at 11:44 AM, Magnus Fromreide &lt;<a>ma...@ly=
sator.liu.se</a>&gt;wrote:
<br>&gt;=20
<br>&gt; &gt; On Sun, Jan 19, 2014 at 07:55:32AM -0800, Peter Bigot wrote:
<br>&gt; &gt; &gt; On Friday, January 17, 2014 10:19:07 PM UTC-6, Marshall =
wrote:
<br>&gt; &gt; &gt; &gt;
<br>&gt; &gt;
<br>&gt; &gt; I have thought in a similar direction, but the problem is if =
you have two
<br>&gt; &gt; string_view's, s1 and s2. Assume that s1 i empty, does the st=
atement
<br>&gt; &gt; s2 =3D s1; imply that s2.data() =3D=3D s1.data()?
<br>&gt; &gt;
<br>&gt; &gt; Then what happens if s1 is deallocated and it's memory is ret=
urned to the
<br>&gt; &gt; system, won't s2.m_ptr then hold an illegal pointer value, on=
e of those
<br>&gt; &gt; where even loading it could trigger a hardware trap on some a=
rchitectures.
<br>&gt; &gt;
<br>&gt;=20
<br>&gt; In my approach I detect in the assignment operator and copy constr=
uctor
<br>&gt; whether the RHS is default-constructed, and if so invoke clear() o=
n the
<br>&gt; LHS/new instance. &nbsp;So in all cases either the reference is to=
 a sequence
<br>&gt; outside the instance, or it's an empty default-constructed (cleare=
d)
<br>&gt; instance where data() is a non-dereferenceable pointer to an inter=
nal charT
<br>&gt; object: no cross-object pointers.
<br>&gt;=20
<br>&gt; Now that you point it out, this does result in behavior non-confor=
mant with
<br>&gt; the current draft specification for the copy constructor and opera=
tor=3D
<br>&gt; which are specified as =3Ddefault.
<br>&gt;=20
<br>&gt; On further reflection it'd better to store a null pointer in m_ptr=
 to
<br>&gt; represent a default-constructed value but to return &amp;m_nul fro=
m data() when
<br>&gt; m_ptr is null. &nbsp;Then the default implementations are retained=
.. &nbsp;This still
<br>&gt; means that data() will be always be non-null, but sv1.data() will =
not
<br>&gt; compare equal to sv2.data() if one or both of the instances have b=
een
<br>&gt; cleared/default-constructed. &nbsp;(sv1 =3D=3D sv2 will still hold=
, of course.)
<br>&gt;=20
<br>&gt; Does that address the objection?
<br>
<br>The check at copy time seems to solve the problem.
<br>
<br>The use of null as a flag value seems to interfere with the "allow null
<br>string_views" </blockquote><div><br>Yes.&nbsp; I am not in favor of str=
ing_view supporting null values as well as empty values; absence of a value=
 is a distinct concept that should be treated independently as with std::op=
tional.&nbsp; I think string_view should support the intersection, not the =
union, of the information provided by its underlying concepts (a) std::stri=
ng and (b) a pair (const charT * ptr, size_t len).&nbsp; Only the latter is=
 capable of expressing absence-of-a-value as distinct from empty-string.&nb=
sp; (In fact it expresses multiple absence-of-a-value as you could wish to =
encode information in a non-zero size() paired with a null data().&nbsp; Ma=
dness that way lies.&nbsp; Just say no to null data().)<br></div></div></bl=
ockquote><div><br>Why would ( nullptr, 3 ) be considered an empty view?&nbs=
p; It's garbage in almost all use cases but, so is ( (char*)1, 3 ).&nbsp; D=
efending against one single bad pointer and ignoring all the rest, solves o=
ne out of a myriad of similar problems.&nbsp; A null view ( null, 0 ) is no=
 different than an empty view ( (char*)rand(), 0 ).&nbsp; Although, is_null=
 seems pointless if it can be guaranteed that the following be true.<br><br=
>For any empty string_view N constructed as ( string_view N{ P, 0 } ), that=
 N.data(), N.begin(), N.end(), etc. all return P.<br><br>Currently P is ban=
ned from being null.&nbsp; Which seems to solve one corner case and, create=
s problems for other, otherwise logical, use cases.<br>&nbsp;<br></div><blo=
ckquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-=
left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr"><div><br>I do thi=
nk that string_view is more a reference type than a collection type (though=
t it is both), so there may be an argument in support of being able to dete=
ct whether an instance holds a valid reference (i.e. does not have a defaul=
t-constructed value, including a value equivalent to default-construction d=
ue to invocation of clear()).&nbsp; If this is desirable, it can be detecte=
d easily by adding a member function has_reference() that returns !!m_ptr.<=
br>&nbsp;</div></div></blockquote><div><br>All references can become dangli=
ng by one way or another.&nbsp; What you described is a smart pointer with =
a string interface, which if that's what you want, I can argue against.&nbs=
p; It may solve many use cases but, seems overkill at least considering the=
 current stated goal of string_view.<br>&nbsp;</div><blockquote class=3D"gm=
ail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #ccc soli=
d;padding-left: 1ex;"><div dir=3D"ltr"><blockquote class=3D"gmail_quote" st=
yle=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1=
ex">and "have an optimized optional&lt;string_view&gt;" proposals
<br>as both allow null's for other purposes.
<br></blockquote><div><br>I can't speak to that question.<br></div><br>Pete=
r<br></div></blockquote></div>

<p></p>

-- <br />
&nbsp;<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 std-proposals+unsubscribe@isocpp.org.<br />
To post to this group, send email to std-proposals@isocpp.org.<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_1627_22142613.1390190865793--

.
