220 28716 <19048c95-770c-4413-b5ff-aa405c389621@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: TONGARI J <tongari95@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Ideas for extending std::tuple with field-name
 based access
Date: Wed, 12 Oct 2016 06:09:19 -0700 (PDT)
Lines: 193
Approved: news@gmane.org
Message-ID: <19048c95-770c-4413-b5ff-aa405c389621@isocpp.org>
References: <CANu6V4VcQQxjb0LvEVjAeNkZ892Gn32cirgVwfjYuAJkkR5EPA@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_8590_506358446.1476277760037"
X-Trace: blaine.gmane.org 1476277799 10850 195.159.176.226 (12 Oct 2016 13:09:59 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Wed, 12 Oct 2016 13:09:59 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBCC3NA775MLBBAHM7C7QKGQEKDZKSPQ@isocpp.org Wed Oct 12 15:09:45 2016
Return-path: <std-proposals+bncBCC3NA775MLBBAHM7C7QKGQEKDZKSPQ@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-yw0-f197.google.com ([209.85.161.197])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBCC3NA775MLBBAHM7C7QKGQEKDZKSPQ@isocpp.org>)
	id 1buJHh-0007xw-Gw
	for gclcip-std-proposals@m.gmane.org; Wed, 12 Oct 2016 15:09:25 +0200
Original-Received: by mail-yw0-f197.google.com with SMTP id u124sf85577315ywg.3
        for <gclcip-std-proposals@m.gmane.org>; Wed, 12 Oct 2016 06:09:27 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id:in-reply-to:references:subject:mime-version
         :x-original-sender:reply-to:precedence:mailing-list:list-id
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=Hi00WHlNyewsJ4MxDf+FpnmjAXV/GMdEbDvd+dBtHlU=;
        b=gcXuLl/IRbZq1804f8CE+Q1hJTSIr3GOeYE6JKueyhY6sIbJWv+WHy918X6LH8zhlt
         bkEdRiRUncptcy5PR4FsHFTT8bC24gtt5cOLyqUb4k+qafFtlN1ycemqVwv63hN7kBXX
         AHszgWN4fPtsRA7kl/TVGyU6lG8BxJSrIpzCu0AQC0kDIE2DW5MIW1v47ejhqxOfUwER
         Wxq3dO22IGjydatPaVaVUMR9ed1lKM1y6GHePxe2vP/ms1n4Mb6c/1o6Ta2ezhsZtAtz
         7/iV4UByaCDFLYtrR0Pc8HJ3IXH3o7kz4ZwmRzFAgWKsdr6Bem8gxmfhKlR76PruAanB
         H1Og==
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
         :x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=Hi00WHlNyewsJ4MxDf+FpnmjAXV/GMdEbDvd+dBtHlU=;
        b=shZ/m5upOxEbmaxqe4Xi86MFGqJAnNjhbyJ50ro9rXADcRcvEFZnZjOd0MM6q9KjP6
         3FjdJY8hAvQDH+8tmY9BuwXpqPiMYIkMoOUAe0i5ZhrXPiwnZB7eHb3ffYcfxJCfrV/W
         P7Y5xiyAkro3iZIVak8XEnMjlB70FOC39/O2lKYmcrqArjUWBDCm2JdfTWUiwuKBkGZd
         0ElsMterd8ZItK1Bi2ugBIdBRRxFYv0VcozZn7OxS/1hvJTBNLUMAPZ/TIMKyXRj027i
         cPMBEo/STisQ1L7Fk8KTlATGHFcx5jGwKnt9mE4b3vbX7Rkjy9LwWc0aBSyZ6f7DtuIx
         VbhQ==
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:x-spam-checked-in-group:list-post:list-help
         :list-archive:list-subscribe:list-unsubscribe;
        bh=Hi00WHlNyewsJ4MxDf+FpnmjAXV/GMdEbDvd+dBtHlU=;
        b=AXtcIfOtaKgO4V+wmwVtCDghrrjAR2FBwu2/SWFkPs2LfVSH/QVEkhVnclTl4SzMhi
         xzjTLYZnNmj7WCWJVFoDSxCs+C1Lx+MIjT+1qrzvwEOrHlSevjqdFPlfCauYwwS3aqYf
         7da8LvPOccUXi/Utg7WLtGGHAB1g17ZqmzhisGHjYvm/ImazGBk+jEVydDzh7EltLWxo
         +T13ZBQ2DotNJ4RehDgh+F5Z0uHGO0RyOpPz0c6zfxcc5VCRpoQGPzYyAk/HO8f/a3EA
         0CLoDV+jnlYwLpThLjf9wm0z3bB3W2Q0og0MYrL2XO+ptbYMDXGweSY+9HYiSkt8xCnI
         /saw==
X-Gm-Message-State: AA6/9RnQjrMZVawuoEzrrv/ZnrfeBsJ0W2ERUyCDFIAaPOXCLXrF+/pNvbOaETUvdElVDQ==
X-Received: by 10.129.110.137 with SMTP id j131mr231247ywc.171.1476277767420;
        Wed, 12 Oct 2016 06:09:27 -0700 (PDT)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.36.227.79 with SMTP id d76ls2552204ith.4.canary; Wed, 12 Oct
 2016 06:09:20 -0700 (PDT)
X-Received: by 10.36.137.67 with SMTP id s64mr124808itd.1.1476277760702;
        Wed, 12 Oct 2016 06:09:20 -0700 (PDT)
In-Reply-To: <CANu6V4VcQQxjb0LvEVjAeNkZ892Gn32cirgVwfjYuAJkkR5EPA@mail.gmail.com>
X-Original-Sender: tongari95@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: <https://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <https://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <https://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <https://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:28716
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/28716>

------=_Part_8590_506358446.1476277760037
Content-Type: multipart/alternative; 
	boundary="----=_Part_8591_1760422009.1476277760038"

------=_Part_8591_1760422009.1476277760038
Content-Type: text/plain; charset=UTF-8

On Wednesday, October 12, 2016 at 7:48:24 PM UTC+8, Johannes Schaub wrote:
>
> Hello all, 
>
> Using type-tags to access tuple fields is a common technique to access 
> fields by "name" 
>
>     struct position_tag : type<double> { }; 
>     struct color_tag : type<QColor> { }; 
>
>     my_tuple<field<position_tag, color_tag> m; 
>
>     QColor c1 = std::get<color_tag>(m); 
>     QColor c2 = std::get<QColor>(m); 
>     QColor c3 = std::get<1>(m); 
>
> I was thinking about extending std::tuple with support for tags. This 
> can be done (i believe) most straight forward with string template 
> arguments, if we would have support for them in the future 
>
>     std::tuple<field<double, "position">, field<QColor, "color">> 
> getStop(); 
>
>     auto m = getStop(); 
>     double pos = std::get<"position">(m); 
>
> An alternative (but uglier) implementation would abuse unnamed struct 
> definitions in template-arguments with the "type" baseclass of above 
> to communicate the type 
>
>     std::tuple<struct : typed<double> { type position; }, 
>                      struct : typed<QColor> { type color; }> getStop(); 
>
> Here, "type" is declared in base-class "typed" as a type-alias for its 
> template-parameter. When tuple sees a parameter has "typed<T>" as a 
> base class, it inherits from it. Therefore you could write 
>
>     auto m = getStop(); 
>     QColor c = m.color; 
>
> std::get<N> would cast the unnamed-struct to "struct_type::type", 
> requiring a standard-layout struct (can be tested-for with a 
> type-trait). So the unnamed-struct cannot "escape" to the users of the 
> tuple. 
>
> What do you think about the two approaches? Are there existing papers 
> about perhaps other techniques? 
>

I'm not aware of any existing proposal, but some experimental language 
features I'm developing will allow you to do this:

std::tuple<double.position, QColor.color> getStop();
auto m = getStop(); 
double pos = m.position; 
QColor c = m.color; 

Let me know if you need more detail.

-- 
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.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/19048c95-770c-4413-b5ff-aa405c389621%40isocpp.org.

------=_Part_8591_1760422009.1476277760038
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Wednesday, October 12, 2016 at 7:48:24 PM UTC+8, Johann=
es Schaub wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;margin=
-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;">Hello all,
<br>
<br>Using type-tags to access tuple fields is a common technique to access
<br>fields by &quot;name&quot;
<br>
<br>=C2=A0 =C2=A0 struct position_tag : type&lt;double&gt; { };
<br>=C2=A0 =C2=A0 struct color_tag : type&lt;QColor&gt; { };
<br>
<br>=C2=A0 =C2=A0 my_tuple&lt;field&lt;position_tag, color_tag&gt; m;
<br>
<br>=C2=A0 =C2=A0 QColor c1 =3D std::get&lt;color_tag&gt;(m);
<br>=C2=A0 =C2=A0 QColor c2 =3D std::get&lt;QColor&gt;(m);
<br>=C2=A0 =C2=A0 QColor c3 =3D std::get&lt;1&gt;(m);
<br>
<br>I was thinking about extending std::tuple with support for tags. This
<br>can be done (i believe) most straight forward with string template
<br>arguments, if we would have support for them in the future
<br>
<br>=C2=A0 =C2=A0 std::tuple&lt;field&lt;double, &quot;position&quot;&gt;, =
field&lt;QColor, &quot;color&quot;&gt;&gt; getStop();
<br>
<br>=C2=A0 =C2=A0 auto m =3D getStop();
<br>=C2=A0 =C2=A0 double pos =3D std::get&lt;&quot;position&quot;&gt;(m);
<br>
<br>An alternative (but uglier) implementation would abuse unnamed struct
<br>definitions in template-arguments with the &quot;type&quot; baseclass o=
f above
<br>to communicate the type
<br>
<br>=C2=A0 =C2=A0 std::tuple&lt;struct : typed&lt;double&gt; { type positio=
n; },
<br>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =
=C2=A0struct : typed&lt;QColor&gt; { type color; }&gt; getStop();
<br>
<br>Here, &quot;type&quot; is declared in base-class &quot;typed&quot; as a=
 type-alias for its
<br>template-parameter. When tuple sees a parameter has &quot;typed&lt;T&gt=
;&quot; as a
<br>base class, it inherits from it. Therefore you could write
<br>
<br>=C2=A0 =C2=A0 auto m =3D getStop();
<br>=C2=A0 =C2=A0 QColor c =3D m.color;
<br>
<br>std::get&lt;N&gt; would cast the unnamed-struct to &quot;struct_type::t=
ype&quot;,
<br>requiring a standard-layout struct (can be tested-for with a
<br>type-trait). So the unnamed-struct cannot &quot;escape&quot; to the use=
rs of the
<br>tuple.
<br>
<br>What do you think about the two approaches? Are there existing papers
<br>about perhaps other techniques?
<br></blockquote><div><br></div><div>I&#39;m not aware of any existing prop=
osal, but some experimental language features I&#39;m developing will allow=
 you to do this:</div><div><br></div><div class=3D"prettyprint" style=3D"ba=
ckground-color: rgb(250, 250, 250); border-color: rgb(187, 187, 187); borde=
r-style: solid; border-width: 1px; word-wrap: break-word;"><code class=3D"p=
rettyprint"><div class=3D"subprettyprint"><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">std</span><span style=3D"color: #660;" class=3D"st=
yled-by-prettify">::</span><span style=3D"color: #000;" class=3D"styled-by-=
prettify">tuple</span><span style=3D"color: #660;" class=3D"styled-by-prett=
ify">&lt;</span><span style=3D"color: #008;" class=3D"styled-by-prettify">d=
ouble</span><span style=3D"color: #660;" class=3D"styled-by-prettify">.</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify">position</span=
><span style=3D"color: #660;" class=3D"styled-by-prettify">,</span><span st=
yle=3D"color: #000;" class=3D"styled-by-prettify"> </span><span style=3D"co=
lor: #606;" class=3D"styled-by-prettify">QColor</span><span style=3D"color:=
 #660;" class=3D"styled-by-prettify">.</span><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify">color</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">&gt;</span><span style=3D"color: #000;" class=3D"st=
yled-by-prettify"> getStop</span><span style=3D"color: #660;" class=3D"styl=
ed-by-prettify">();</span></div><div class=3D"subprettyprint"><span style=
=3D"color: #000;" class=3D"styled-by-prettify"></span><span style=3D"color:=
 #008;" class=3D"styled-by-prettify">auto</span><span style=3D"color: #000;=
" class=3D"styled-by-prettify"> m </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> getStop</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">();</span><span style=3D"color: #000;" class=3D"styled-by-pr=
ettify"> <br></span><span style=3D"color: #008;" class=3D"styled-by-prettif=
y">double</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> =
pos </span><span style=3D"color: #660;" class=3D"styled-by-prettify">=3D</s=
pan><span style=3D"color: #000;" class=3D"styled-by-prettify"> m</span><spa=
n style=3D"color: #660;" class=3D"styled-by-prettify">.</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify">position</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: #60=
6;" class=3D"styled-by-prettify">QColor</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> c </span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">=3D</span><span style=3D"color: #000;" class=3D"sty=
led-by-prettify"> m</span><span style=3D"color: #660;" class=3D"styled-by-p=
rettify">.</span><span style=3D"color: #000;" class=3D"styled-by-prettify">=
color</span><span style=3D"color: #660;" class=3D"styled-by-prettify">;</sp=
an><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span></div>=
</code></div><div><br>Let me know if you need more detail.</div></div>

<p></p>

-- <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 <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
To view this discussion on the web visit <a href=3D"https://groups.google.c=
om/a/isocpp.org/d/msgid/std-proposals/19048c95-770c-4413-b5ff-aa405c389621%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/19048c95-770c-4413-b5ff-aa405c389621=
%40isocpp.org</a>.<br />

------=_Part_8591_1760422009.1476277760038--

------=_Part_8590_506358446.1476277760037--

.
