220 8651 <CANh-dXnYg=1kmm1nJdSnTXmYyfBxbLTD9b+wecT+eZm5nPkf2A@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Jeffrey Yasskin <jyasskin@google.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: string_view::is_null()
Date: Fri, 17 Jan 2014 12:11:33 -0800
Lines: 87
Approved: news@gmane.org
Message-ID: <CANh-dXnYg=1kmm1nJdSnTXmYyfBxbLTD9b+wecT+eZm5nPkf2A@mail.gmail.com>
References: <8045a4d2-721d-4725-8bb7-7a91b6f53ec8@isocpp.org>
 <186E0927-C962-4E34-AED0-E7509DD1F0A0@gmail.com> <7aad44d8-6b4e-438a-b309-bd898b994b34@isocpp.org>
 <9C2C439F-7983-4224-B272-53CAD0E2E456@gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: quoted-printable
X-Trace: ger.gmane.org 1389989509 1899 80.91.229.3 (17 Jan 2014 20:11:49 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 17 Jan 2014 20:11:49 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDDM34EO6QDRBCU542LAKGQELEVMEUI@isocpp.org Fri Jan 17 21:11:57 2014
Return-path: <std-proposals+bncBDDM34EO6QDRBCU542LAKGQELEVMEUI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-vb0-f71.google.com ([209.85.212.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDDM34EO6QDRBCU542LAKGQELEVMEUI@isocpp.org>)
	id 1W4Flj-0006ND-Q8
	for gclcip-std-proposals@m.gmane.org; Fri, 17 Jan 2014 21:11:56 +0100
Original-Received: by mail-vb0-f71.google.com with SMTP id p5sf7024690vbn.6
        for <gclcip-std-proposals@m.gmane.org>; Fri, 17 Jan 2014 12:11:54 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=google.com; s=20120113;
        h=mime-version:in-reply-to:references:from:date:message-id:subject: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
         :content-transfer-encoding;
        bh=nGE4c/fUT9EOCseKN6+i1RPdnaF+JSmk5PErZezYobk=;
        b=QLcgqhatuNxg1CSQ7oDf9uv7GRjAV2qi55w8h6t4/9j24FdgVNk/oGBUSDVRDCDUjg
         eBMEtMR4p8p7KeTzNjaCXs+9HO7QXJL7pIWKMcY7iwQEkUzB/QvySkKaTjUqEwTnJqz0
         CcWsDEoxTFjK93CAU31fZHnMPB6Tn9OhI+aUqD9Fe0N+0YarB0PZnVQMFZFfYWrGSdze
         15u+P9w8iHxEQ8cLowJ7EaEOzCUH4t/FmP2Qs0VxrFOfuvp5MeBLkYlE6t4udXeRgO9F
         +slq6zi//f79j0K6hfaMNo3AWx/2sffzHYTotuodjH1a78l7CPXj8ce4/gY5RJk4fLwp
         Q9yA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:from:date
         :message-id:subject: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:content-transfer-encoding;
        bh=nGE4c/fUT9EOCseKN6+i1RPdnaF+JSmk5PErZezYobk=;
        b=VNUqiSBLrHrpwzcjEHcHClcVYiQJF7XfuhH4SgAO0QCmv+YyrsN5EqRkm6yBVCdoHj
         xZkJBUA6ZNE1bT5nF/IM3lq1LHJ3j86YPOfFfjSQlzMiS2E3vPgF1Wq6wozvShnKtOlp
         qYqZhPYcdQz7FTyNwzN/NbgNSZLsH97l7ryE5OZ0sagCdaD/yZSEtVQD5GWtXc7KrIoZ
         CKtsPyBIgBdJ7k67IvoEANL59hZJVHuWtoo7E/b5t217FaVudwZfgxoZ2hU6rkfEMkS6
         Om7g110E60Lkgf+R85tju83NmL5j/DcqLHYhSlVYC4z/pyNdHN3zVTEXFxDjsNgxQPVy
         XxuQ==
X-Gm-Message-State: ALoCoQk8quYinc3iqMId3sGB4te09s0E2XgpALVy4l91tSHrKoh+npo66nZY8qRDQZhXVaHhLOOP
X-Received: by 10.58.34.142 with SMTP id z14mr1315515vei.23.1389989514895;
        Fri, 17 Jan 2014 12:11:54 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.127.46 with SMTP id nd14ls1271110qeb.19.gmail; Fri, 17 Jan
 2014 12:11:54 -0800 (PST)
X-Received: by 10.224.88.70 with SMTP id z6mr6839257qal.14.1389989514087;
        Fri, 17 Jan 2014 12:11:54 -0800 (PST)
Original-Received: from mail-qa0-x231.google.com (mail-qa0-x231.google.com [2607:f8b0:400d:c00::231])
        by mx.google.com with ESMTPS id b79si3131702qge.79.2014.01.17.12.11.54
        for <std-proposals@isocpp.org>
        (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128);
        Fri, 17 Jan 2014 12:11:54 -0800 (PST)
Received-SPF: pass (google.com: domain of jyasskin@google.com designates 2607:f8b0:400d:c00::231 as permitted sender) client-ip=2607:f8b0:400d:c00::231;
Original-Received: by mail-qa0-f49.google.com with SMTP id w8so3648519qac.22
        for <std-proposals@isocpp.org>; Fri, 17 Jan 2014 12:11:53 -0800 (PST)
X-Received: by 10.224.30.133 with SMTP id u5mr6575655qac.47.1389989513818;
 Fri, 17 Jan 2014 12:11:53 -0800 (PST)
Original-Received: by 10.229.208.138 with HTTP; Fri, 17 Jan 2014 12:11:33 -0800 (PST)
In-Reply-To: <9C2C439F-7983-4224-B272-53CAD0E2E456@gmail.com>
X-Original-Sender: jyasskin@google.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of jyasskin@google.com designates 2607:f8b0:400d:c00::231 as permitted
 sender) smtp.mail=jyasskin@google.com;       dkim=pass header.i=@google.com;
       dmarc=pass (p=REJECT dis=NONE) header.from=google.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:8651
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/8651>

On Fri, Jan 17, 2014 at 8:26 AM, Marshall Clow <mclow.lists@gmail.com> wrot=
e:
> On Jan 15, 2014, at 4:28 AM, Olaf van der Spek <olafvdspek@gmail.com> wro=
te:
>
> On Saturday, December 28, 2013 6:01:34 AM UTC+1, Marshall wrote:
>>
>> if sv.size () =3D=3D 0 or sv.empty () [same thing], there is nothing tha=
t you
>> can do with sv.data()
>> (except compare it to null, and that doesn=92t tell you anything useful)=
..
>
>
> Are you sure?
> IMO an empty string_view could still point into a string and I should be
> able to use that pointer to for example construct a new string_view.
>
>
> A string view references a half-open contiguous range of characters; [
> data(), data()+size())
>
> if size() =3D=3D 0, then the range of characters referenced by the string=
 view
> is empty, and accessing them (either via dereferencing begin() or data())=
 is
> (or should be) undefined behavior. (the same as any other container)
>
> If in your program, you =93know=94 that the pointer returned by data() po=
ints to
> something valid, well, that=92s on you to determine.
>
> Code example:
>
> char deref ( string_view sv ) { return *sv.data(); }
>
> string_view sv1;
> string_view sv2 { =93ABCDE=94 };
> sv2.remove_prefix(5);
> assert ( sv1 =3D=3D sv2 );
> deref ( sv1 ); // may or may not blow up in your face
> deref ( sv2 ); // this will probably =93work=94 ; but I don=92t know what=
 it will
> return.

The string_view spec guarantees the value data() returns after
substr() and remove_prefix() operations, so the 'sv2' case is actually
guaranteed to return the '\0' at the end of "ABCDE". However,
*sv2.begin() has undefined behavior, because it goes through the
iterator type. The use I intended for this was the ability to write
things like:

const char str[] =3D "ABCDE";
string_view sv{str};
consume_letters(sv);
if (sv.data() =3D=3D std::end(str))
  finished();

but I don't see a way to enable that without letting people also
dereference data() when the string_view is empty but they know
something about the value. (This assumes that knowing ptr1=3D=3Dptr2 means
that *ptr1 and *ptr2 are equally valid, which isn't the case for
things like realloc(), but I don't want to put any such corner cases
in the string_view spec if I can help it.)

I do see that this is a somewhat awkward middle ground, so I'm
definitely going to ask for a straw-poll in Issaquah about whether the
LWG wants data()=3D=3Dnullptr to be allowed. If it is, I intend to also
remove the precondition on the string_view(data, size) to let people
explicitly put nullptr in. I'm not sure if string_view() should be
guaranteed to produce data()=3D=3Dnullptr if that change happens.

It'll also be possible to change this after the first version of the
TS ships with string_view inside, if field experience indicates that
it's the wrong choice.

--=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/.

.
