Topic: Stronger Version of typedef


Author: =?UTF-8?Q?Jens_=C3=85kerblom?= <akerblom.jens@gmail.com>
Date: Sat, 25 Feb 2017 03:14:21 -0800 (PST)
Raw View
------=_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--

.


Author: Nicol Bolas <jmckesson@gmail.com>
Date: Sat, 25 Feb 2017 07:49:26 -0800 (PST)
Raw View
------=_Part_2651_1824980059.1488037766486
Content-Type: multipart/alternative;
 boundary="----=_Part_2652_785832217.1488037766486"

------=_Part_2652_785832217.1488037766486
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

On Saturday, February 25, 2017 at 6:14:21 AM UTC-5, Jens =C3=85kerblom wrot=
e:
>
> Is there any proposal to have a more "strongly typed" version of typedef?
>

In the time it took you to make this post, you could have done a Google=20
search on "C++ strong typedef proposal"; the first hit is one of many such=
=20
proposals. Though a number of them also use the terms "opaque typedef" or=
=20
"strong alias".

In any case, the general problem all of them have encountered is defining=
=20
the scope of exactly what "all functionality of the type you'd like to=20
typedef" should be. C++ is unlike many languages in that the core=20
functionality of a type doesn't always live within the scope of that type.=
=20
That is, we have a lot of non-member functions that manipulate a type=20
(overloaded operators and the like), so any strong/opaque typedef proposal=
=20
needs to deal with that. Plus, code the compiler sees *after* the strong=20
typedef can add new functionality to a type, so that too needs to be=20
accounted for.

Then there's the specific question of typedef strength. The standard=20
`typedef/using` is just an alias, but there are many levels of strength=20
above that. Sometimes, you want to create a new type but allow it to be=20
implicitly convertible to the old type. Sometimes, you want to create a new=
=20
type but allow it to be *explicitly* convertible to the old type. And=20
sometimes, you want a complete separation from the old type. There are=20
probably some levels inbetween (public code can only explicitly convert,=20
while private code can implicitly convert).
=20

> Something like the Ada functionality above.
>

--=20
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 e=
mail 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/99897a20-8476-498a-9b66-58c831168cdb%40isocpp.or=
g.

------=_Part_2652_785832217.1488037766486
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">On Saturday, February 25, 2017 at 6:14:21 AM UTC-5, Jens =
=C3=85kerblom wrote:<blockquote class=3D"gmail_quote" style=3D"margin: 0;ma=
rgin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=
=3D"ltr">Is there any proposal to have a more &quot;strongly typed&quot; ve=
rsion of typedef?</div></blockquote><div><br>In the time it took you to mak=
e this post, you could have done a Google search on &quot;C++ strong typede=
f proposal&quot;; the first hit is one of many such proposals. Though a num=
ber of them also use the terms &quot;opaque typedef&quot; or &quot;strong a=
lias&quot;.<br><br>In any case, the general problem all of them have encoun=
tered is defining the scope of exactly what &quot;all functionality of the =
type you&#39;d like to typedef&quot; should be. C++ is unlike many language=
s in that the core functionality of a type doesn&#39;t always live within t=
he scope of that type. That is, we have a lot of non-member functions that =
manipulate a type (overloaded operators and the like), so any strong/opaque=
 typedef proposal needs to deal with that. Plus, code the compiler sees <i>=
after</i> the strong typedef can add new functionality to a type, so that t=
oo needs to be accounted for.<br><br>Then there&#39;s the specific question=
 of typedef strength. The standard `typedef/using` is just an alias, but th=
ere are many levels of strength above that. Sometimes, you want to create a=
 new type but allow it to be implicitly convertible to the old type. Someti=
mes, you want to create a new type but allow it to be <i>explicitly</i> con=
vertible to the old type. And sometimes, you want a complete separation fro=
m the old type. There are probably some levels inbetween (public code can o=
nly explicitly convert, while private code can implicitly convert).<br>=C2=
=A0</div><blockquote class=3D"gmail_quote" style=3D"margin: 0;margin-left: =
0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir=3D"ltr">Some=
thing like the Ada functionality above.<br></div></blockquote></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/99897a20-8476-498a-9b66-58c831168cdb%=
40isocpp.org?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/99897a20-8476-498a-9b66-58c831168cdb=
%40isocpp.org</a>.<br />

------=_Part_2652_785832217.1488037766486--

------=_Part_2651_1824980059.1488037766486--

.


Author: "Vicente J. Botet Escriba" <vicente.botet@wanadoo.fr>
Date: Sat, 25 Feb 2017 22:26:39 +0100
Raw View
This is a multi-part message in MIME format.
--------------441A912F53B042AE930E4C2C
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: quoted-printable

Le 25/02/2017 =C3=A0 12:14, Jens =C3=85kerblom a =C3=A9crit :
> Using typedef can be a very good way of simplifying the code by having=20
> the new type name indicate the usage, for example:
>
> |
> typedeffloatradians_t;
> typedeffloatdegrees_t;
> typedefintfoo_id_t;
> typedefintbar_id_t;
>
> voidsetAngle(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 =3D45.0f;
> foo_id_t fooId =3D5;
> bar_id_t barId =3D8;
>
> // These "should" probably all be errors:
> setAngle(angle);
> Foo& foo =3D getFoo(barId);
> Bar& bar =3D getBar(fooId);
> |
>
> One way to work around this is to create a new type (using struct or=20
> class) and implement all functionality of the type you'd like to=20
> typedef. This does however force the programmer to create a lot of=20
> code and since it's only for catching error cases it doesn't happen=20
> 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 =3D angle2; // Error (unless user-defined cast operator exists).
> |
>
These kind of stong typedefs should be managed by a library as Boost.Unit.
> Ada has the feature that you can subtype as:
>
> |
> type INT is new Integer;
> a : INT;
> b : Integer;
>
> a :=3D b; <- ERROR
> |
>
I've a strong_integer class that allows you to do

using INT =3D  strong_integer<class INT_tag, int>;

which doesn't converts implicitly to int, is explicitly convertible from=20
int  and provides the builtin operations.
If this is enough to you, there is a simple library solution. I believe=20
that this is already better than using int all around.

I believe that we should have something like that in C++,  at least for=20
the integral builtin-types.

I did a prototype of a library (TBoost.Opaque) that allows to define=20
more explicit operations, but I've never used it in a product.
> Is there any proposal to have a more "strongly typed" version of=20
> typedef? Something like the Ada functionality above.

Look for opaque in the list of C++ proposals.

Vicente

--=20
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 e=
mail 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/5d3bfdf0-2132-64e0-3b9f-47f3a5710d3d%40wanadoo.f=
r.

--------------441A912F53B042AE930E4C2C
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<html>
  <head>
    <meta content=3D"text/html; charset=3Dutf-8" http-equiv=3D"Content-Type=
">
  </head>
  <body bgcolor=3D"#FFFFFF" text=3D"#000000">
    <div class=3D"moz-cite-prefix">Le 25/02/2017 =C3=A0 12:14, Jens =C3=85k=
erblom a
      =C3=A9crit=C2=A0:<br>
    </div>
    <blockquote
      cite=3D"mid:cb540c63-e37e-4c86-ab7f-6d5791aa70fc@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr">Using typedef can be a very good way of simplifying
        the code 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"subprettyprint"><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"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"styled-by-prettify">
                degrees_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">int</sp=
an><span
                style=3D"color: #000;" class=3D"styled-by-prettify">
                foo_id_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">int</sp=
an><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"styled-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-prettify">
                setAngle</span><span style=3D"color: #660;"
                class=3D"styled-by-prettify">(</span><span style=3D"color:
                #000;" class=3D"styled-by-prettify">radians_t angle</span><=
span
                style=3D"color: #660;" class=3D"styled-by-prettify">);</spa=
n><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
              </span><span style=3D"color: #606;"
                class=3D"styled-by-prettify">Foo</span><span style=3D"color=
:
                #660;" class=3D"styled-by-prettify">&amp;</span><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> getFoo=
</span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">(</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify">foo_id_=
t
                id</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">Bar</span><span style=3D"color=
:
                #660;" class=3D"styled-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-prettify">);</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:
          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"><code class=3D"prettyprint"><span
                  style=3D"color: #000;" class=3D"styled-by-prettify">degre=
es</span><span
                  style=3D"color: #000;" class=3D"styled-by-prettify">_t </=
span></code><span
                style=3D"color: #000;" class=3D"styled-by-prettify">angle <=
/span><span
                style=3D"color: #660;" class=3D"styled-by-prettify">=3D</sp=
an><span
                style=3D"color: #000;" class=3D"styled-by-prettify"> </span=
><span
                style=3D"color: #066;" class=3D"styled-by-prettify">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_id_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
                style=3D"color: #660;" class=3D"styled-by-prettify">;</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                bar_id_t barId </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">8</span=
><span
                style=3D"color: #660;" class=3D"styled-by-prettify">;</span=
><span
                style=3D"color: #000;" class=3D"styled-by-prettify"><br>
                <br>
                // These "should" 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">);</spa=
n><span
                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"color:
                #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'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:<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-prettify">struct radians_t<br>
                {<br>
                =C2=A0=C2=A0=C2=A0 float v;<br>
                };<br>
                <br>
                bool operator&lt;(radians_t, radians_t);<br>
              </span>radians_t operator+(radians_t, radians_t);<br>
              <code class=3D"prettyprint"><span style=3D"color: #606;"
                  class=3D"styled-by-prettify"><code class=3D"prettyprint">=
<span
                      class=3D"styled-by-prettify"></span></code>// etc...<=
/span></code><br>
              <span style=3D"color: #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"><s=
pan
                    class=3D"styled-by-prettify"><code class=3D"prettyprint=
"><span
                        class=3D"styled-by-prettify">degrees_t</span></code=
></span></code>,
              </span><span style=3D"color: #606;"
                class=3D"styled-by-prettify"><code class=3D"prettyprint"><s=
pan
                    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"prettyprint"><s=
pan
                    class=3D"styled-by-prettify"><code class=3D"prettyprint=
"><span
                        class=3D"styled-by-prettify">degrees_t </span></cod=
e></span></code>operator+(</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">degrees_t</span></code=
></span></code>,
              </span><span style=3D"color: #606;"
                class=3D"styled-by-prettify"><code class=3D"prettyprint"><s=
pan
                    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></code>// etc...<br>
                <br>
                radians_t angle1;<br>
                degrees_t angle2;<br>
                <br>
                angle1 =3D angle2; // Error (unless user-defined cast
                operator exists).<br>
              </span><span style=3D"color: #660;"
                class=3D"styled-by-prettify"></span></div>
          </code></div>
        <br>
      </div>
    </blockquote>
    These kind of stong typedefs should be managed by a library as
    Boost.Unit.<br>
    <blockquote
      cite=3D"mid:cb540c63-e37e-4c86-ab7f-6d5791aa70fc@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr">Ada has the feature that you can subtype 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-prettify">type 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"></span></div>
          </code></div>
        <br>
      </div>
    </blockquote>
    I've a strong_integer class that allows you to do<br>
    <br>
    using INT =3D=C2=A0 strong_integer&lt;class INT_tag, int&gt;;<br>
    <br>
    which doesn't converts implicitly to int, is explicitly convertible
    from int=C2=A0 and provides the builtin operations.<br>
    If this is enough to you, there is a simple library solution. I
    believe that this is already better than using int all around.<br>
    <br>
    I believe that we should have something like that in C++,=C2=A0 at leas=
t
    for the integral builtin-types.<br>
    <br>
    I did a prototype of a library (TBoost.Opaque) that allows to define
    more explicit operations, but I've never used it in a product.<br>
    <blockquote
      cite=3D"mid:cb540c63-e37e-4c86-ab7f-6d5791aa70fc@isocpp.org"
      type=3D"cite">
      <div dir=3D"ltr">Is there any proposal to have a more "strongly
        typed" version of typedef? Something like the Ada functionality
        above.<br>
      </div>
    </blockquote>
    <br>
    Look for opaque in the list of C++ proposals.<br>
    <br>
    Vicente<br>
  </body>
</html>

<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/5d3bfdf0-2132-64e0-3b9f-47f3a5710d3d%=
40wanadoo.fr?utm_medium=3Demail&utm_source=3Dfooter">https://groups.google.=
com/a/isocpp.org/d/msgid/std-proposals/5d3bfdf0-2132-64e0-3b9f-47f3a5710d3d=
%40wanadoo.fr</a>.<br />

--------------441A912F53B042AE930E4C2C--

.