220 31130 <cb540c63-e37e-4c86-ab7f-6d5791aa70fc@isocpp.org> article
Path: news.gmane.org!.POSTED!not-for-mail
From: =?UTF-8?Q?Jens_=C3=85kerblom?= <akerblom.jens@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Stronger Version of typedef
Date: Sat, 25 Feb 2017 03:14:21 -0800 (PST)
Lines: 221
Approved: news@gmane.org
Message-ID: <cb540c63-e37e-4c86-ab7f-6d5791aa70fc@isocpp.org>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: blaine.gmane.org
Mime-Version: 1.0
Content-Type: multipart/mixed; 
	boundary="----=_Part_468_818244683.1488021261577"
X-Trace: blaine.gmane.org 1488021261 21849 195.159.176.226 (25 Feb 2017 11:14:21 GMT)
X-Complaints-To: usenet@blaine.gmane.org
NNTP-Posting-Date: Sat, 25 Feb 2017 11:14:21 +0000 (UTC)
To: ISO C++ Standard - Future Proposals <std-proposals@isocpp.org>
Original-X-From: std-proposals+bncBC2YT3FOXEIBBDWOYXCQKGQENPEBEDA@isocpp.org Sat Feb 25 12:14:17 2017
Return-path: <std-proposals+bncBC2YT3FOXEIBBDWOYXCQKGQENPEBEDA@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-ot0-f198.google.com ([74.125.82.198])
	by blaine.gmane.org with esmtp (Exim 4.84_2)
	(envelope-from <std-proposals+bncBC2YT3FOXEIBBDWOYXCQKGQENPEBEDA@isocpp.org>)
	id 1chaIr-00056n-8r
	for gclcip-std-proposals@m.gmane.org; Sat, 25 Feb 2017 12:14:17 +0100
Original-Received: by mail-ot0-f198.google.com with SMTP id y13sf68761714oty.2
        for <gclcip-std-proposals@m.gmane.org>; Sat, 25 Feb 2017 03:14:23 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp-org.20150623.gappssmtp.com; s=20150623;
        h=date:from:to:message-id: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=/X1KnEH38ORy/Ymrjw8fCFbAy0Bc9tk0uoVMiOfwsDw=;
        b=W9vvtyqxny+xhJqPH/V76xJl+4uEfmDe8fUtMe244stq9mmoW0FyoljzMG9HyKKL3I
         bE+lVQDMwxP6fUdipvORn9aVBzW/hO04ZXzsCnsf4TO6ZQVmVbpm9SgBttB8gMS0FPoK
         W/xUn2ziXQuoGX5HvvMhHYgLXRuSp/ObZNMRc7KnI116mmMPwAJw2RNUpuYRsqevvi35
         u8xfJHggmQbPElPHOSfR1z3ycTh+j4Fyhadrbo9d0R5ctAVGHHS+lIqtpjx6ojBElaLU
         oxwaLVYwJ6BAqSw0HErgoA2GIfWmAkBnQzKVJ9H8z4T3/c+KhtUfKGZoUFQGHQgFgdU3
         bgqw==
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20161025;
        h=date:from:to:message-id: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=/X1KnEH38ORy/Ymrjw8fCFbAy0Bc9tk0uoVMiOfwsDw=;
        b=jm6F4s5yaQ0MxTBd/7FW1g9REsYIoQwxxPgK0W4wLJal0v6iZOlZpP0AJVL0TybNF1
         IP9CDJOaDQGNanXP0NB55eGFnpSrHFRRYSFlbfFybMtTgX8lfXW9senCbFmNKjATMuLk
         rqnRFh9tOMd4cvIOpWManGJyptZQaFjlBrlaJkj1VS3QlKPRqZKkD1sXZjKlc/HkTsc8
         tvzXjGNvKJ6eobDrBXptADhFtLb4Cmnvfe96IgXVe2HKAUlbiQa9R1Qz4qdSfTmYN6LI
         lu9CZg0vnUhceI9IxeXtKMHzCZDZCQELXf1yftcaevM92CJa59QM5ourvIZTmEBYIc58
         cCIA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20161025;
        h=x-gm-message-state:date:from:to:message-id: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=/X1KnEH38ORy/Ymrjw8fCFbAy0Bc9tk0uoVMiOfwsDw=;
        b=R8c0n5rystU6Ikm8qqEYP3Pll4gjp0R5Y8Q5PKPl8YiRxdY/hu4d9tfSa6ZCZZ4PLz
         EuEGjfuVWkQeVZihVA4U/DurL6j6tHx1XxY9Oo+bp7movN9CIjjGlw2SAmuCQHShFLWH
         AMfQI6ldIysu30IwHUJAbEaBXHJIuteHF5b2zzUabPbaGFb77qyGiNhOHkH9OmvZQMty
         ayC1WO41J1Q/JgXt0IY0nanG6o0AhRtn6XRa4ktVzkmzSWaci1tgL04tin4K4C7tq9Gf
         Kox05ttxJQaR2xM3vhBpDT2WUhhXqw/wiBlSl9Pa8hmZ+b1D4G9sI1bXK7BDSlb/ZsYG
         uKtw==
X-Gm-Message-State: AMke39kU9ANabQLVSKzCZkAR4kbJakWh0rwEKW7NPt99GTM8VsNxWk53zbFSCq11o3Hw6w==
X-Received: by 10.157.50.45 with SMTP id t45mr2557602otc.43.1488021262725;
        Sat, 25 Feb 2017 03:14:22 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.157.42.168 with SMTP id e37ls9234646otb.13.gmail; Sat, 25 Feb
 2017 03:14:22 -0800 (PST)
X-Received: by 10.157.82.86 with SMTP id q22mr516322otg.3.1488021262096;
        Sat, 25 Feb 2017 03:14:22 -0800 (PST)
X-Original-Sender: akerblom.jens@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:31130
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/31130>

------=_Part_468_818244683.1488021261577
Content-Type: multipart/alternative; 
	boundary="----=_Part_469_593012567.1488021261578"

------=_Part_469_593012567.1488021261578
Content-Type: text/plain; charset=UTF-8

Using typedef can be a very good way of simplifying the code by having the 
new type name indicate the usage, for example:

typedef float radians_t;
typedef float degrees_t;
typedef int foo_id_t;
typedef int bar_id_t;

void setAngle(radians_t angle);
Foo& getFoo(foo_id_t id);
Bar& getBar(bar_id_t bar);

However, this is a very weak typing. Consider

degrees_t angle = 45.0f;
foo_id_t fooId = 5;
bar_id_t barId = 8;

// These "should" probably all be errors:
setAngle(angle);
Foo& foo = getFoo(barId);
Bar& bar = getBar(fooId);

One way to work around this is to create a new type (using struct or class) 
and implement all functionality of the type you'd like to typedef. This 
does however force the programmer to create a lot of code and since it's 
only for catching error cases it doesn't happen that often. Such as:

struct radians_t
{
    float v;
};

bool operator<(radians_t, radians_t);
radians_t operator+(radians_t, radians_t);
// etc...

struct degrees_t
{
    float v;
};

bool operator<(degrees_t, degrees_t);
degrees_t operator+(degrees_t, degrees_t);
// etc...

radians_t angle1;
degrees_t angle2;

angle1 = angle2; // Error (unless user-defined cast operator exists).

Ada has the feature that you can subtype as:

type INT is new Integer;
a : INT;
b : Integer;

a := b; <- ERROR

Is there any proposal to have a more "strongly typed" version of typedef? 
Something like the Ada functionality above.

-- 
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/cb540c63-e37e-4c86-ab7f-6d5791aa70fc%40isocpp.org.

------=_Part_469_593012567.1488021261578
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Using typedef can be a very good way of simplifying the co=
de by having the new type name indicate the usage, for example:<br><br><div=
 style=3D"background-color: rgb(250, 250, 250); border-color: rgb(187, 187,=
 187); border-style: solid; border-width: 1px; overflow-wrap: break-word;" =
class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"subprettypr=
int"><span style=3D"color: #008;" class=3D"styled-by-prettify">typedef</spa=
n><span style=3D"color: #000;" class=3D"styled-by-prettify"> </span><span s=
tyle=3D"color: #008;" class=3D"styled-by-prettify">float</span><span style=
=3D"color: #000;" class=3D"styled-by-prettify"> radians_t</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: #008=
;" class=3D"styled-by-prettify">typedef</span><span style=3D"color: #000;" =
class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" class=3D"=
styled-by-prettify">float</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"> degrees_t</span><span style=3D"color: #660;" class=3D"style=
d-by-prettify">;</span><span style=3D"color: #000;" class=3D"styled-by-pret=
tify"><br></span><span style=3D"color: #008;" class=3D"styled-by-prettify">=
typedef</span><span style=3D"color: #000;" class=3D"styled-by-prettify"> </=
span><span style=3D"color: #008;" class=3D"styled-by-prettify">int</span><s=
pan style=3D"color: #000;" class=3D"styled-by-prettify"> foo_id_t</span><sp=
an 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: #008;" class=3D"styled-by-prettify">typedef</span><span style=3D"color=
: #000;" class=3D"styled-by-prettify"> </span><span style=3D"color: #008;" =
class=3D"styled-by-prettify">int</span><span style=3D"color: #000;" class=
=3D"styled-by-prettify"> bar_id_t</span><span style=3D"color: #660;" class=
=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"style=
d-by-prettify"><br><br></span><span style=3D"color: #008;" class=3D"styled-=
by-prettify">void</span><span style=3D"color: #000;" class=3D"styled-by-pre=
ttify"> setAngle</span><span style=3D"color: #660;" class=3D"styled-by-pret=
tify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">rad=
ians_t angle</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: #606;" class=3D"styled-by-prettify">Foo</span><s=
pan style=3D"color: #660;" class=3D"styled-by-prettify">&amp;</span><span s=
tyle=3D"color: #000;" class=3D"styled-by-prettify"> getFoo</span><span styl=
e=3D"color: #660;" class=3D"styled-by-prettify">(</span><span style=3D"colo=
r: #000;" class=3D"styled-by-prettify">foo_id_t id</span><span style=3D"col=
or: #660;" class=3D"styled-by-prettify">);</span><span style=3D"color: #000=
;" class=3D"styled-by-prettify"><br></span><span style=3D"color: #606;" cla=
ss=3D"styled-by-prettify">Bar</span><span style=3D"color: #660;" class=3D"s=
tyled-by-prettify">&amp;</span><span style=3D"color: #000;" class=3D"styled=
-by-prettify"> getBar</span><span style=3D"color: #660;" class=3D"styled-by=
-prettify">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify=
">bar_id_t bar</span><span style=3D"color: #660;" class=3D"styled-by-pretti=
fy">);</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>=
</span></div></code></div><br>However, this is a very weak typing. Consider=
<br><br><div style=3D"background-color: rgb(250, 250, 250); border-color: r=
gb(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: b=
reak-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D=
"subprettyprint"><code class=3D"prettyprint"><span style=3D"color: #000;" c=
lass=3D"styled-by-prettify">degrees</span><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">_t </span></code><span style=3D"color: #000;" clas=
s=3D"styled-by-prettify">angle </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: #066;" class=3D"styled-by-prett=
ify">45.0f</span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
;</span><span style=3D"color: #000;" class=3D"styled-by-prettify"><br>foo_i=
d_t fooId </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: #066;" class=3D"styled-by-prettify">5</span><span st=
yle=3D"color: #660;" class=3D"styled-by-prettify">;</span><span style=3D"co=
lor: #000;" class=3D"styled-by-prettify"><br>bar_id_t barId </span><span st=
yle=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: #0=
66;" class=3D"styled-by-prettify">8</span><span style=3D"color: #660;" clas=
s=3D"styled-by-prettify">;</span><span style=3D"color: #000;" class=3D"styl=
ed-by-prettify"><br><br>// These &quot;should&quot; probably all be errors:=
<br>setAngle</span><span style=3D"color: #660;" class=3D"styled-by-prettify=
">(</span><span style=3D"color: #000;" class=3D"styled-by-prettify">angle</=
span><span style=3D"color: #660;" class=3D"styled-by-prettify">);</span><sp=
an style=3D"color: #000;" class=3D"styled-by-prettify"><br>Foo&amp; foo =3D=
 getFoo(barId);<br>Bar&amp; bar =3D getBar(fooId);</span><span style=3D"col=
or: #800;" class=3D"styled-by-prettify"></span><span style=3D"color: #000;"=
 class=3D"styled-by-prettify"><br></span></div></code></div><br>One
 way to work around this is to create a new type (using struct or class)
 and implement all functionality of the type you&#39;d like to typedef. Thi=
s
 does however force the programmer to create a lot of code and since=20
it&#39;s only for catching error cases it doesn&#39;t happen that often. Su=
ch=20
as:<br><br><div style=3D"background-color: rgb(250, 250, 250); border-color=
: rgb(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap=
: break-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=
=3D"subprettyprint"><span style=3D"color: #606;" class=3D"styled-by-prettif=
y">struct radians_t<br>{<br>=C2=A0=C2=A0=C2=A0 float v;<br>};<br><br>bool o=
perator&lt;(radians_t, radians_t);<br></span>radians_t operator+(radians_t,=
 radians_t);<br><code class=3D"prettyprint"><span style=3D"color: #606;" cl=
ass=3D"styled-by-prettify"><code class=3D"prettyprint"><span class=3D"style=
d-by-prettify"></span></code>// etc...</span></code><br><span style=3D"colo=
r: #606;" class=3D"styled-by-prettify"><br>struct degrees_t<br>{<br>=C2=A0=
=C2=A0=C2=A0 float v;<br>};<br><br>bool operator&lt;(</span><span style=3D"=
color: #606;" class=3D"styled-by-prettify"><code class=3D"prettyprint"><spa=
n class=3D"styled-by-prettify"><code class=3D"prettyprint"><span class=3D"s=
tyled-by-prettify">degrees_t</span></code></span></code>, </span><span styl=
e=3D"color: #606;" class=3D"styled-by-prettify"><code class=3D"prettyprint"=
><span class=3D"styled-by-prettify"><code class=3D"prettyprint"><span class=
=3D"styled-by-prettify">degrees_t</span></code></span></code>);<br></span><=
span style=3D"color: #606;" class=3D"styled-by-prettify"><code class=3D"pre=
ttyprint"><span class=3D"styled-by-prettify"><code class=3D"prettyprint"><s=
pan class=3D"styled-by-prettify">degrees_t </span></code></span></code>oper=
ator+(</span><span style=3D"color: #606;" class=3D"styled-by-prettify"><cod=
e class=3D"prettyprint"><span class=3D"styled-by-prettify"><code class=3D"p=
rettyprint"><span class=3D"styled-by-prettify">degrees_t</span></code></spa=
n></code>, </span><span style=3D"color: #606;" class=3D"styled-by-prettify"=
><code class=3D"prettyprint"><span class=3D"styled-by-prettify"><code class=
=3D"prettyprint"><span class=3D"styled-by-prettify"><code class=3D"prettypr=
int"><span class=3D"styled-by-prettify">degrees_t</span></code></span></cod=
e>);<br></span></code>// etc...<br><br>radians_t angle1;<br>degrees_t angle=
2;<br><br>angle1 =3D angle2; // Error (unless user-defined cast operator ex=
ists).<br></span><span style=3D"color: #660;" class=3D"styled-by-prettify">=
</span></div></code></div><br>Ada has the feature that you can subtype as:<=
br><br><div style=3D"background-color: rgb(250, 250, 250); border-color: rg=
b(187, 187, 187); border-style: solid; border-width: 1px; overflow-wrap: br=
eak-word;" class=3D"prettyprint"><code class=3D"prettyprint"><div class=3D"=
subprettyprint"><span style=3D"color: #606;" class=3D"styled-by-prettify">t=
ype INT is new Integer;<br>a : INT;<br>b : Integer;<br><br>a :=3D b; &lt;- =
ERROR</span><span style=3D"color: #660;" class=3D"styled-by-prettify"></spa=
n></div></code></div><br>Is there any proposal to have a more &quot;strongl=
y typed&quot; version of typedef? Something like the Ada functionality abov=
e.<br></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/cb540c63-e37e-4c86-ab7f-6d5791aa70fc%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/cb540c63-e37e-4c86-ab7f-6d5791aa70fc=
%40isocpp.org</a>.<br />

------=_Part_469_593012567.1488021261578--

------=_Part_468_818244683.1488021261577--

.
