Topic: typedef typename


Author: maxtal@Physics.usyd.edu.au (John Max Skaller)
Date: 1995/08/08
Raw View
In article <1995Jul19.120503.13951@leeds.ac.uk>,
Gary Thompson <garyt@resumix.portal.com> wrote:
>The only question I wanted to ask about the putative standard is why there is still no provision for creating a new type.
...
>typedef long miles;
>typedef long kilometers;
>typedef long yards;

 I guess this is because the world of C hackery is concerned
with efficiency and not safety.

 The opposite viewpoint is that this solution is very specific
and thus quite weak, and so isn't worth an extension.

 You _can_ hack a solution if you design your own types
with this in mind. For example:

 template<class T> class C { .. ordinary class here .. };
 enum X {};
 enum Y {};
 C<X> cx;
 C<Y> cy; // structurally equivalent but distinct types

Of course, this doesn't work for built-in types and can be
a hassle to retrofit. I'm not sure how many compilers would be smart
enough to recognize the same binary object code can be used for
both classes.

This problem is quite serious in scientific programming where,
for example, one want to avoid mistakes with matrices operating
with different bases on the same space, or even on distinct spaces.

See Barton and Nackman for more information on how to design
this kind of thing into your classes systematically.

BTW: a hack that may work in future is:

 namespace Metres {
  #include "number.stuff"
 }
 namespace Feet {
  #include "number stuff"
 }
 // :-))

--
        JOHN (MAX) SKALLER,         INTERNET:maxtal@suphys.physics.su.oz.au
 Maxtal Pty Ltd,
        81A Glebe Point Rd, GLEBE   Mem: SA IT/9/22,SC22/WG21
        NSW 2037, AUSTRALIA     Phone: 61-2-566-2189





Author: garyt@resumix.portal.com (Gary Thompson)
Date: 1995/07/19
Raw View
The only question I wanted to ask about the putative standard is why there is still no provision for creating a new type.

eg:

Apple defines fixed as a unsigned long, but it is used as a 16.16 fixed point number

if an object has constructtors or operators that take unsigned longs it is very easy to pass in a fixed poinmt number by accident leading to all sorts of errors, or vice versa

another example you write a program with the following typedefs

typedef long miles;
typedef long kilometers;
typedef long yards;

it is quite possible to pass miles to functions taking kilometers and so forth
(of course it would be possible to write a LONG class (which miles kilometers and yards subclass) that emulated a long fully but this would be very long winded, and error prone I think??)

these are just a few examples

as fars as I know this problem has not been solved, is there a reason why?? I feel this especially a lack in a language that uses so much strong typing.

gary

---
________________________________________________________________________________

Dr G. S. Thompson.   quote of the day/week/month/eternity:
Biomolecular Structure Group.
Department of Chemistry.
University of Leeds.   "If everyone stands on tiptoes noone
Leeds LS2 9JT.     can see any better..."

Phone 0113 233 6578.
e-mail: garyt@chem.leeds.ac.uk
_______________________________________________________________________________







Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1995/07/19
Raw View
In article 13951@leeds.ac.uk, garyt@resumix.portal.com (Gary Thompson) writes:
>The only question I wanted to ask about the putative standard is why there is still no provision for creating a new type.

A rather strong statement, false on its face. You have always been able to
create new types in C++.

>eg:
>
>Apple defines fixed as a unsigned long, but it is used as a 16.16 fixed point number
>
>if an object has constructtors or operators that take unsigned longs it is very easy to pass in a fixed poinmt number by accident leading to all sorts of errors, or vice versa

That is why the 'explicit' keyword was added. A constructor declared
'explicit' can be used to perform a type conversion only when explicitly
invoked. Example:
 class miles {
 public:
  explicit miles(unsigned long);
  ...
 };
 void foo(miles);
 ...
 foo(10); // ERROR, no matching function
 foo(miles(10)); // OK, explicit type conversion

>
>another example you write a program with the following typedefs
>
>typedef long miles;
>typedef long kilometers;
>typedef long yards;
>
>it is quite possible to pass miles to functions taking kilometers and so forth ...

A typedef does not create a new type, so you should not use typedefs when
you want to have distinct types. Typedefs are useful, but that isn't one
of their uses.

---
Steve Clamage, stephen.clamage@eng.sun.com