220 2807 <41bc2377-7654-43f4-83f1-133edeca18d3@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: Mon, 11 Feb 2013 03:01:41 -0800 (PST)
Lines: 197
Approved: news@gmane.org
Message-ID: <41bc2377-7654-43f4-83f1-133edeca18d3@isocpp.org>
References: <913f9603-aff2-4abf-a330-d4d0f8a2d1e0@isocpp.org>
 <5a76aa73-1940-4247-b40d-c7c1ba972246@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_949_33418972.1360580502048"
X-Trace: ger.gmane.org 1360580503 26055 80.91.229.3 (11 Feb 2013 11:01:43 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 11 Feb 2013 11:01:43 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBCEKFTV6ZUMBBF474OEAKGQEP76PJCQ@isocpp.org Mon Feb 11 12:02:04 2013
Return-path: <std-proposals+bncBCEKFTV6ZUMBBF474OEAKGQEP76PJCQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ie0-f198.google.com ([209.85.223.198])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBCEKFTV6ZUMBBF474OEAKGQEP76PJCQ@isocpp.org>)
	id 1U4r99-00080B-Sr
	for gclcip-std-proposals@m.gmane.org; Mon, 11 Feb 2013 12:02:04 +0100
Original-Received: by mail-ie0-f198.google.com with SMTP id 17sf26131926iea.1
        for <gclcip-std-proposals@m.gmane.org>; Mon, 11 Feb 2013 03:01:43 -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=7eFkeIb8MMejDTfXVBVfDjZ9ZwvopsvhEengK3fySN8=;
        b=rCh/kl3MlvAjY4r+RwdjsB2It84u6lDbV09VZ3wVDiw9TwW5WF2nWrzzXUjwHbt8ZL
         5HBFPHjR+GZPCf0iJyb86zoK3bKunmLLbiGGym2P+t9HdyIVPBX5Ls+z3h/L9gvCTW83
         Rh6JQhXcpUsZdtIlHuHsYcbVqw0uhB5c1YPXnPlFZZ7yB7LJhxbQjxO/OqJXbcaqYfPo
         hnXlVSGUVCNzYVBUSqcm8E2HtR4W0edafHttxk4VDRBw/XwSxnDbZqFjkUUOnFT/4RLI
         /tZFfqYgm5zokx3nZpnYom1xZMJIySEFqTpb6uGqYImtKiju3PdNnbxfnb6mDSqHXoas
         ZZgQ==
X-Received: by 10.50.159.193 with SMTP id xe1mr8295342igb.0.1360580503710;
        Mon, 11 Feb 2013 03:01:43 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.50.5.175 with SMTP id t15ls1591089igt.33.canary; Mon, 11 Feb
 2013 03:01:43 -0800 (PST)
X-Received: by 10.50.149.132 with SMTP id ua4mr272379igb.8.1360580503015;
        Mon, 11 Feb 2013 03:01:43 -0800 (PST)
In-Reply-To: <5a76aa73-1940-4247-b40d-c7c1ba972246@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:2807
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/2807>

------=_Part_949_33418972.1360580502048
Content-Type: text/plain; charset=ISO-8859-1



On Monday, February 11, 2013 1:46:18 AM UTC-8, Nikolay Ivchenkov wrote:
>
> On Monday, February 11, 2013 6:52:27 AM UTC+4, Nicol Bolas wrote:
>>
>>
>> 2: We can use template metaprogramming to differentiate between someone 
>> passing any old `const charT*` and an actual string literal. This could be 
>> useful for std::string implementations, where they could store an allocated 
>> string or a string_literal, depending on how it was initialized. Obviously 
>> non-const member accesses will cause a copy of the string, similar to COW, 
>> but without the general dangers of that construct.
>>
>
> std::basic_string cannot be implemented so. Expression str[0] (where str 
> is an instance of std::string) shall not throw exceptions and the equality 
> str.c_str() == &str[0] shall always hold:
>
>     void f(std::string &str)
>     {
>         char const *p1 = str.c_str();
>         char *p2 = &str[0];
>         if (!str.empty())
>             *p2 = '?';
>         assert(p1 == p2);
>     }
>

Fair enough.
 

>
> 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 ar3[] = {'n', 'o', 't', ' ', 'l', 'i', 't', 'e', 'r', 'a', 
'l'};

That would work just as well with your way.

-- 

--- 
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_949_33418972.1360580502048
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<br><br>On Monday, February 11, 2013 1:46:18 AM UTC-8, Nikolay Ivchenkov wr=
ote:<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, 20=
13 6:52:27 AM UTC+4, Nicol Bolas wrote:<blockquote class=3D"gmail_quote" st=
yle=3D"margin:0;margin-left:0.8ex;border-left:1px #ccc solid;padding-left:1=
ex"><br>2: We can use template metaprogramming to differentiate between som=
eone passing any old `const charT*` and an actual string literal. This coul=
d be useful for std::string implementations, where they could store an allo=
cated string or a string_literal, depending on how it was initialized. Obvi=
ously non-const member accesses will cause a copy of the string, similar to=
 COW, but without the general dangers of that construct.<br></blockquote><d=
iv><br>std::basic_string cannot be implemented so. Expression str[0] (where=
 str is an instance of std::string) shall not throw exceptions and the equa=
lity str.c_str() =3D=3D &amp;str[0] shall always hold:<br><br>&nbsp;&nbsp;&=
nbsp; void f(std::string &amp;str)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char const *p1 =3D str.c_str();<br>&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *p2 =3D &amp;str[0];<br>&nbsp;&nbsp;&=
nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!str.empty())<br>&nbsp;&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *p2 =3D '?';<br>&nbsp;&nbsp;&nbs=
p;&nbsp;&nbsp;&nbsp;&nbsp; assert(p1 =3D=3D p2);<br>&nbsp;&nbsp;&nbsp; }<br=
></div></blockquote><div><br>Fair enough.<br>&nbsp;</div><blockquote class=
=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: 1px #cc=
c solid;padding-left: 1ex;"><div><br>It would be possible to improve classe=
s for immutable strings if parameters could be declared with 'static conste=
xpr' and the overload resolution could select appropriate function dependin=
g on whether the argument is known to be a constexpr object with static sto=
rage duration:<br><br>&nbsp;&nbsp;&nbsp; template &lt;class CharT&gt;<br>&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; class basic_immutable_string<br>&n=
bsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; public:<br>&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp;&nbsp;&nbsp; template &lt;std::size_t N&gt;<br>&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; basic_immutable_string(static=
 constexpr CharT (&amp;s)[N]); // #1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp=
;&nbsp; basic_immutable_string(CharT const *s); // #2<br>&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp; ....<br>&nbsp;&nbsp;&nbsp; };<br><br>&nbsp;&nbsp;&=
nbsp; using immutable_string =3D basic_immutable_string&lt;char&gt;;<br><br=
>&nbsp;&nbsp;&nbsp; template &lt;std::size_t N&gt;<br>&nbsp;&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp; void f(static constexpr char (&amp;x)[N])<br>&nbsp;&n=
bsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static constexpr=
 char a1[] =3D "text";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static=
 char a2[] =3D "text";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&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;&nbsp;&nbsp;&nbsp;=
 immutable_string s1 =3D a1; // calls #1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&=
nbsp;&nbsp; immutable_string s2 =3D a2; // calls #2<br>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp; immutable_string s3 =3D ra1; // calls #2<br>&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; immutable_string s4 =3D "text"; // calls=
 #1<br>&nbsp;&nbsp;&nbsp; }<br></div><br></blockquote><div><br>Not quite:<b=
r><br><div class=3D"prettyprint" style=3D"background-color: rgb(250, 250, 2=
50); border-color: rgb(187, 187, 187); border-style: solid; border-width: 1=
px; word-wrap: break-word;"><code class=3D"prettyprint"><div class=3D"subpr=
ettyprint"><span style=3D"color: #008;" class=3D"styled-by-prettify">static=
</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><s=
pan style=3D"color: #008;" class=3D"styled-by-prettify">constexpr</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"> ar3</span><span sty=
le=3D"color: #660;" class=3D"styled-by-prettify">[]</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #660=
;" class=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify"> </span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">{</span><span style=3D"color: #080;" class=3D"styled-by-pre=
ttify">'n'</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #080;" class=3D"styled-by-prettify">'o'</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080=
;" class=3D"styled-by-prettify">'t'</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pre=
ttify">' '</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #080;" class=3D"styled-by-prettify">'l'</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080=
;" class=3D"styled-by-prettify">'i'</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pre=
ttify">'t'</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #080;" class=3D"styled-by-prettify">'e'</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">,</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #080=
;" class=3D"styled-by-prettify">'r'</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">,</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"> </span><span style=3D"color: #080;" class=3D"styled-by-pre=
ttify">'a'</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
,</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><=
span style=3D"color: #080;" class=3D"styled-by-prettify">'l'</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></span></div></code></div><br=
>That would work just as well with your way.<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_949_33418972.1360580502048--

.
