220 2806 <5a76aa73-1940-4247-b40d-c7c1ba972246@isocpp.org> article
Path: news.gmane.org!not-for-mail
From: Nikolay Ivchenkov <mk.ivchenkov@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Improvements for string literals
Date: Mon, 11 Feb 2013 01:46:18 -0800 (PST)
Lines: 133
Approved: news@gmane.org
Message-ID: <5a76aa73-1940-4247-b40d-c7c1ba972246@isocpp.org>
References: <913f9603-aff2-4abf-a330-d4d0f8a2d1e0@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_201_17904782.1360575978691"
X-Trace: ger.gmane.org 1360575981 17721 80.91.229.3 (11 Feb 2013 09:46:21 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Mon, 11 Feb 2013 09:46:21 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDJK3XNWR4LRB2734KEAKGQEF6IYTYQ@isocpp.org Mon Feb 11 10:46:42 2013
Return-path: <std-proposals+bncBDJK3XNWR4LRB2734KEAKGQEF6IYTYQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-gg0-f199.google.com ([209.85.161.199])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDJK3XNWR4LRB2734KEAKGQEF6IYTYQ@isocpp.org>)
	id 1U4pyB-0002dh-2r
	for gclcip-std-proposals@m.gmane.org; Mon, 11 Feb 2013 10:46:39 +0100
Original-Received: by mail-gg0-f199.google.com with SMTP id v5sf4484422ggn.6
        for <gclcip-std-proposals@m.gmane.org>; Mon, 11 Feb 2013 01:46:19 -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=zYtJM7oyJ0k7rQ/VoX/0eMKkYsLWVvAcYaHEa1C4f2M=;
        b=KU/H/Wte0ESHhQsDvqqOq6StmDYUtBLbd1T2qcLWud2DnIKdU9Rf/OyMGfqP+2vyXB
         G3nGYbkSEbunc18vt/Zu2ZF6O3n8FMgOMvtBnfofRB5OaG4Sw25bMXVgp3FmpbsZ55+Q
         E4wzbNqmuHDejMhOba4Fm1YiEHEumd5BhhFwYZkWdmu8N/BObif+d1W/j90qjnBd8VkD
         hf9cwyQ2BJUAsEkA4ktDGBfr6j/jnq4m4u2ARyWU2Sp19wG7Yif4xULr+PCGgJ3P7PUq
         3iTLZPYnEMVkxoFdUtZa2nZSsx3K6k3TQDMu3GEGUpqAjJ8oTh6qem1YpyUkEFuVapCr
         qbsw==
X-Received: by 10.236.130.141 with SMTP id k13mr6355106yhi.19.1360575979312;
        Mon, 11 Feb 2013 01:46:19 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.49.24.37 with SMTP id r5ls1684120qef.85.gmail; Mon, 11 Feb
 2013 01:46:18 -0800 (PST)
X-Received: by 10.49.58.167 with SMTP id s7mr877884qeq.5.1360575978930;
        Mon, 11 Feb 2013 01:46:18 -0800 (PST)
In-Reply-To: <913f9603-aff2-4abf-a330-d4d0f8a2d1e0@isocpp.org>
X-Original-Sender: mk.ivchenkov@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:2806
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/2806>

------=_Part_201_17904782.1360575978691
Content-Type: text/plain; charset=ISO-8859-1

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);
    }

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
    }

-- 

--- 
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_201_17904782.1360575978691
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On Monday, February 11, 2013 6:52:27 AM UTC+4, Nicol Bolas wrote:<blockquot=
e class=3D"gmail_quote" style=3D"margin: 0;margin-left: 0.8ex;border-left: =
1px #ccc solid;padding-left: 1ex;"><br>2: We can use template metaprogrammi=
ng to differentiate between someone passing any old `const charT*` and an a=
ctual 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 tha=
t construct.<br></blockquote><div><br>std::basic_string cannot be implement=
ed so. Expression str[0] (where str is an instance of std::string) shall no=
t throw exceptions and the equality str.c_str() =3D=3D &amp;str[0] shall al=
ways 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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; char *p2 =
=3D &amp;str[0];<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (!str.emp=
ty())<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=
 *p2 =3D '?';<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; assert(p1 =3D=
=3D p2);<br>&nbsp;&nbsp;&nbsp; }<br><br>It would be possible to improve cla=
sses for immutable strings if parameters could be declared with 'static con=
stexpr' and the overload resolution could select appropriate function depen=
ding 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_immutable_string<br=
>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; public:<br>&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp;&nbsp;&nbsp; template &lt;std::size_t N&gt;<br>&nbsp;&nbsp;&nbsp;=
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; basic_immutable_string(sta=
tic constexpr CharT (&amp;s)[N]); // #1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n=
bsp;&nbsp; basic_immutable_string(CharT const *s); // #2<br>&nbsp;&nbsp;&nb=
sp;&nbsp;&nbsp;&nbsp;&nbsp; ....<br>&nbsp;&nbsp;&nbsp; };<br><br>&nbsp;&nbs=
p;&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;=
&nbsp;&nbsp;&nbsp;&nbsp; void f(static constexpr char (&amp;x)[N])<br>&nbsp=
;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; static conste=
xpr char a1[] =3D "text";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sta=
tic char a2[] =3D "text";<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; aut=
o &amp;ra1 =3D a1;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; immuta=
ble_string s0 =3D x; // calls #1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb=
sp; immutable_string s1 =3D a1; // calls #1<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbs=
p;&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"; // ca=
lls #1<br>&nbsp;&nbsp;&nbsp; }<br></div><br>

<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_201_17904782.1360575978691--

.
