220 2824 <91d78642-a52e-4c45-b02f-096c35306487@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Nicol Bolas <jmckesson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Improvements for string literals
Date: Wed, 13 Feb 2013 16:09:22 -0800 (PST)
Lines: 285
Approved: news@gmane.org
Message-ID: <91d78642-a52e-4c45-b02f-096c35306487@isocpp.org>
References: <913f9603-aff2-4abf-a330-d4d0f8a2d1e0@isocpp.org>
 <5a76aa73-1940-4247-b40d-c7c1ba972246@isocpp.org>
 <41bc2377-7654-43f4-83f1-133edeca18d3@isocpp.org>
 <7ebf708e-87f3-4e68-9921-c80f862c63a4@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_116_23604752.1360800562616"
X-Trace: ger.gmane.org 1360800563 13034 80.91.229.3 (14 Feb 2013 00:09:23 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Thu, 14 Feb 2013 00:09:23 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBM6W6CEAKGQECAO7ANI@isocpp.org Thu Feb 14 01:09:44 2013
Return-path: <std-proposals+bncBCEKFTV6ZUMBBM6W6CEAKGQECAO7ANI@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-oa0-f72.google.com ([209.85.219.72])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBM6W6CEAKGQECAO7ANI@isocpp.org>)
	id 1U5mOW-0001i2-3w
	for gclcip-std-proposals@m.gmane.org; Thu, 14 Feb 2013 01:09:44 +0100
Original-Received: by mail-oa0-f72.google.com with SMTP id j6sf9221127oag.11
        for <gclcip-std-proposals@m.gmane.org>; Wed, 13 Feb 2013 16:09:24 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=x-received:x-beenthere:x-received:date:from:to:message-id
         :in-reply-to:references:subject:mime-version:x-original-sender
         :reply-to:precedence:mailing-list:list-id:x-google-group-id
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe
         :content-type;
        bh=Zm4os5TmST9/fV0FYxCI81kzgsv4htxHR/vOeKjrD6s=;
        b=GfHN53+FbTsFLJkOXheceZWGNdB50NegyZ4k3/hJ71YoDN5z74FeyHELMAw+Zts7Zz
         TgpMvpXaDurlsShwqsPTAvMQz9V77mI6mbQfASzjqEwoOdn+bS0jrpcN1Rywipsh4Ziq
         n3xQ4OJzhAiJW2QMct4KHN+TI2h2ppj1bbun8BTPrTSYISCT39WfADsrpsKctgZERsi9
         XSbRS5wiIZuKGbf9fH/RwHMjwiy+dO8k3AhEgbglJU8TQ3EgtXuElh9tWl0VYIc2l2BG
         I9I2BAhu7Zqj7/2kfncLT/IGEsA29P0b6fI6VYQ1l/ogDMwA/yv/ADae6A6ccY2w5YST
         t6Wg==
X-Received: by 10.182.167.66 with SMTP id zm2mr3868311obb.7.1360800564055;
        Wed, 13 Feb 2013 16:09:24 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.182.23.111 with SMTP id l15ls129273obf.12.gmail; Wed, 13 Feb
 2013 16:09:22 -0800 (PST)
X-Received: by 10.182.189.67 with SMTP id gg3mr24820obc.30.1360800562881;
        Wed, 13 Feb 2013 16:09:22 -0800 (PST)
In-Reply-To: <7ebf708e-87f3-4e68-9921-c80f862c63a4@isocpp.org>
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: <http://groups.google.com/a/isocpp.org/group/std-proposals/post?hl=en>,
 <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?hl=en&topic=25838>,
 <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/?hl=en>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe?hl=en>,
 <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:2824
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/2824>

------=_Part_116_23604752.1360800562616
Content-Type: text/plain; charset=ISO-8859-1



On Wednesday, February 13, 2013 2:40:35 PM UTC-8, Nikolay Ivchenkov wrote:
>
> On Monday, February 11, 2013 3:01:41 PM UTC+4, Nicol Bolas wrote:
>>
>> It would be possible to improve classes for immutable strings if 
>>> parameters could be declared with 'static constexpr' and the overload 
>>> resolution could select appropriate function depending on whether the 
>>> argument is known to be a constexpr object with static storage duration:
>>>
>>>     template <class CharT>
>>>         class basic_immutable_string
>>>     {
>>>     public:
>>>         template <std::size_t N>
>>>             basic_immutable_string(static constexpr CharT (&s)[N]); // #1
>>>         basic_immutable_string(CharT const *s); // #2
>>>         ....
>>>     };
>>>
>>>     using immutable_string = basic_immutable_string<char>;
>>>
>>>     template <std::size_t N>
>>>         void f(static constexpr char (&x)[N])
>>>     {
>>>         static constexpr char a1[] = "text";
>>>         static char a2[] = "text";
>>>         auto &ra1 = a1;
>>>
>>>         immutable_string s0 = x; // calls #1
>>>         immutable_string s1 = a1; // calls #1
>>>         immutable_string s2 = a2; // calls #2
>>>         immutable_string s3 = ra1; // calls #2
>>>         immutable_string s4 = "text"; // calls #1
>>>     }
>>>
>>>
>> Not quite:
>>
>> static constexpr char ar3[] = {'n', 'o', 't', ' ', 'l', 'i', 't', 'e', 
>> 'r', 'a', 'l'};
>>
>> That would work just as well with your way.
>>
>
> If we remove the constexpr specifier, then initialization
>
>     immutable_string s = ar3;
>
> will not be well-defined, because ar3 is not a null-terminated string. In 
> the presence of constexpr the effect of such initialization could be 
> considered as undefined similarly.
>

Even if it were NULL-terminated, it wouldn't be a *string literal* anymore. 
Thus, you could not guarantee that it is a string literal. And therefore, 
you couldn't make certain assumptions about it. For example, this is 
well-defined C++ code:

const char *GetLiteral()
{
    const char *ar = "blah";
    return ar;
}

This is not.

const char *GetLiteral2()
{
  const char ar[] = {'n', 'o', 't', ' ', 'l', 'i', 't', 'e', 'r', 'a', 'l', 
'\0'};
  return ar;
}

String literals are required to be stored in permanent memory, effectively 
part of the executable. Arbitrary `const char*`'s are not. What I want is a 
type that, *in all cases*, is required to come only from actual literals. 
It's not just that the string is "immutable". It is important that the 
string come from a string literal as defined by the standard.

Being able to create strings with "static constexpr" and whatnot is not the 
issue here. String literals are not the only source of `char*`s that can be 
"static constexpr"s. So that won't help.

You're looking for a general class of immutable strings, which may be the 
result of various operations. I'm talking about real, honest-to-God string 
literals, as defined by the standard. Not the result of various constexpr 
computations or {} initializations or whatever.

-- 

--- 
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/?hl=en.



------=_Part_116_23604752.1360800562616
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br><br>On Wednesday, February 13, 2013 2:40:35 PM UTC-8, Nikolay Ivchenkov=
 wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.=
8ex;border-left: 1px #ccc solid;padding-left: 1ex;">On Monday, February 11,=
 2013 3:01:41 PM UTC+4, Nicol Bolas wrote:<blockquote class=3D"gmail_quote"=
 style=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-lef=
t:1ex"><blockquote class=3D"gmail_quote" style=3D"margin:0;margin-left:0.8e=
x;border-left:1px #ccc solid;padding-left:1ex"><div>It would be possible to=
 improve classes for immutable strings if parameters could be declared with=
 'static constexpr' and the overload resolution could select appropriate fu=
nction depending on whether the argument is known to be a constexpr object =
with static storage duration:<br><br>&nbsp;&nbsp;&nbsp; template &lt;class =
CharT&gt;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class basic_immutab=
le_string<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; public:<br>&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; template &lt;std::size_t N&gt;<br>&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; basic_immutabl=
e_string(static constexpr CharT (&amp;s)[N]); // #1<br>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp; basic_immutable_string(CharT const *s); // #2<br>&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ....<br>&nbsp;&nbsp;&nbsp; };<br><b=
r>&nbsp;&nbsp;&nbsp; using immutable_string =3D basic_immutable_string&lt;c=
har&gt;;<br><br>&nbsp;&nbsp;&nbsp; template &lt;std::size_t N&gt;<br>&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void f(static constexpr char (&amp;x)[=
N])<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s=
tatic constexpr char a1[] =3D "text";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp; static char a2[] =3D "text";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&nbsp; auto &amp;ra1 =3D a1;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp; immutable_string s0 =3D x; // calls #1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp; immutable_string s1 =3D a1; // calls #1<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp; immutable_string s2 =3D a2; // calls #2<br>&nbsp=
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; immutable_string s3 =3D ra1; // calls=
 #2<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; immutable_string s4 =3D "=
text"; // calls #1<br>&nbsp;&nbsp;&nbsp; }<br></div><br></blockquote><div><=
br>Not quite:<br><br><div style=3D"background-color:rgb(250,250,250);border=
-color:rgb(187,187,187);border-style:solid;border-width:1px;word-wrap:break=
-word"><code><div><span style=3D"color:#008">static</span><span style=3D"co=
lor:#000"> </span><span style=3D"color:#008">constexpr</span><span style=3D=
"color:#000"> char ar3</span><span style=3D"color:#660">[]</span><span styl=
e=3D"color:#000"> </span><span style=3D"color:#660">=3D</span><span style=
=3D"color:#000"> </span><span style=3D"color:#660">{</span><span style=3D"c=
olor:#080">'n'</span><span style=3D"color:#660">,</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#080">'o'</span><span style=3D"color:#=
660">,</span><span style=3D"color:#000"> </span><span style=3D"color:#080">=
't'</span><span style=3D"color:#660">,</span><span style=3D"color:#000"> </=
span><span style=3D"color:#080">' '</span><span style=3D"color:#660">,</spa=
n><span style=3D"color:#000"> </span><span style=3D"color:#080">'l'</span><=
span style=3D"color:#660">,</span><span style=3D"color:#000"> </span><span =
style=3D"color:#080">'i'</span><span style=3D"color:#660">,</span><span sty=
le=3D"color:#000"> </span><span style=3D"color:#080">'t'</span><span style=
=3D"color:#660">,</span><span style=3D"color:#000"> </span><span style=3D"c=
olor:#080">'e'</span><span style=3D"color:#660">,</span><span style=3D"colo=
r:#000"> </span><span style=3D"color:#080">'r'</span><span style=3D"color:#=
660">,</span><span style=3D"color:#000"> </span><span style=3D"color:#080">=
'a'</span><span style=3D"color:#660">,</span><span style=3D"color:#000"> </=
span><span style=3D"color:#080">'l'</span><span style=3D"color:#660">};</sp=
an><span style=3D"color:#000"><br></span></div></code></div><br>That would =
work just as well with your way.<br></div></blockquote><div><br>If we remov=
e the constexpr specifier, then initialization<br><br>&nbsp;&nbsp;&nbsp; im=
mutable_string s =3D ar3;<br><br>will not be well-defined, because ar3 is n=
ot a null-terminated string. In the presence of constexpr the effect of suc=
h initialization could be considered as undefined similarly.<br></div></blo=
ckquote><div><br>Even if it were NULL-terminated, it wouldn't be a <i>strin=
g literal</i> anymore. Thus, you could not guarantee that it is a string li=
teral. And therefore, you couldn't make certain assumptions about it. For e=
xample, this is well-defined C++ code:<br><br><div class=3D"prettyprint" st=
yle=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187, 18=
7); border-style: solid; border-width: 1px; word-wrap: break-word;"><code c=
lass=3D"prettyprint"><div class=3D"subprettyprint"><span style=3D"color: #0=
08;" class=3D"styled-by-prettify">const</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">char</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> </span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">*</span><span style=3D"color: #606;" class=3D"styled-by-prettify">GetL=
iteral</span><span style=3D"color: #660;" class=3D"styled-by-prettify">()</=
span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br></span><=
span style=3D"color: #660;" class=3D"styled-by-prettify">{</span><span styl=
e=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">const</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"c=
olor: #008;" class=3D"styled-by-prettify">char</span><span style=3D"color: =
#000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660;" cl=
ass=3D"styled-by-prettify">*</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify">ar </span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">=3D</span><span style=3D"color: #000;" class=3D"styled-by-pretti=
fy"> </span><span style=3D"color: #080;" class=3D"styled-by-prettify">"blah=
"</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</span><=
span style=3D"color: #000;" class=3D"styled-by-prettify"><br>&nbsp; &nbsp; =
</span><span style=3D"color: #008;" class=3D"styled-by-prettify">return</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> ar</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"><br></span><span style=3D"co=
lor: #660;" class=3D"styled-by-prettify">}</span></div></code></div><br>Thi=
s is not.<br><br><div class=3D"prettyprint" style=3D"background-color: rgb(=
250, 250, 250); border-color: rgb(187, 187, 187); border-style: solid; bord=
er-width: 1px; word-wrap: break-word;"><code class=3D"prettyprint"><div cla=
ss=3D"subprettyprint"><span style=3D"color: #008;" class=3D"styled-by-prett=
ify">const</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
 </span><span style=3D"color: #008;" class=3D"styled-by-prettify">char</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #660;" class=3D"styled-by-prettify">*</span><span style=3D"c=
olor: #606;" class=3D"styled-by-prettify">GetLiteral2</span><span style=3D"=
color: #660;" class=3D"styled-by-prettify">()</span><span style=3D"color: #=
000;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" =
class=3D"styled-by-prettify">{</span><span style=3D"color: #000;" class=3D"=
styled-by-prettify"><br>&nbsp; </span><span style=3D"color: #008;" class=3D=
"styled-by-prettify">const</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #008;" class=3D"styled-by-pre=
ttify">char</span><span style=3D"color: #000;" class=3D"styled-by-prettify"=
> ar</span><span style=3D"color: #660;" class=3D"styled-by-prettify">[]</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span =
style=3D"color: #660;" class=3D"styled-by-prettify">=3D</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color=
: #660;" class=3D"styled-by-prettify">{</span><span style=3D"color: #080;" =
class=3D"styled-by-prettify">'n'</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pret=
tify">'o'</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #080;" class=3D"styled-by-prettify">'t'</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;=
" class=3D"styled-by-prettify">' '</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pret=
tify">'l'</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #080;" class=3D"styled-by-prettify">'i'</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;=
" class=3D"styled-by-prettify">'t'</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pret=
tify">'e'</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #080;" class=3D"styled-by-prettify">'r'</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"col=
or: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080;=
" class=3D"styled-by-prettify">'a'</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pret=
tify">'l'</span><span style=3D"color: #660;" class=3D"styled-by-prettify">,=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #080;" class=3D"styled-by-prettify">'\0'</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">};</span><span style=3D"c=
olor: #000;" class=3D"styled-by-prettify"><br>&nbsp; </span><span style=3D"=
color: #008;" class=3D"styled-by-prettify">return</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify"> ar</span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"><br></span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">}</span></div></code></div><br>String literals are requir=
ed to be stored in permanent memory, effectively part of the executable. Ar=
bitrary `const char*`'s are not. What I want is a type that, <i>in all case=
s</i>, is required to come only from actual literals. It's not just that th=
e string is "immutable". It is important that the string come from a string=
 literal as defined by the standard.<br><br>Being able to create strings wi=
th "static constexpr" and whatnot is not the issue here. String literals ar=
e not the only source of `char*`s that can be "static constexpr"s. So that =
won't help.<br><br>You're looking for a general class of immutable strings,=
 which may be the result of various operations. I'm talking about real, hon=
est-to-God string literals, as defined by the standard. Not the result of v=
arious constexpr computations or {} initializations or whatever.<br></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/?hl=3Den">http://groups.google.com/a/isocpp.org/group/std-pro=
posals/?hl=3Den</a>.<br />
&nbsp;<br />
&nbsp;<br />

------=_Part_116_23604752.1360800562616--

.
