Article View: comp.std.c++
Article #3415Re: typedef not strong
From: Tim Hollebeek
Date: Thu, 22 Feb 1996 23:53
Date: Thu, 22 Feb 1996 23:53
84 lines
4158 bytes
4158 bytes
Harlan Messinger (gusty@clark.net) wrote:
: Fergus Henderson (fjh@munta.cs.mu.OZ.AU) wrote:
: : rpayne@rainbow.rmii.com (Robert Payne) writes:
: :
: : >Why has C++ stayed with the weak typedef? It has always seemed to
: : >me that it should provide a new type and not just a synomym. Some
: : >lints check for strong typing but I haven't found a compiler that
: : >will enforce it. I know this must have been debated at some point
: : >but I didn't find it in a FAQ. Could someone please enlighten me?
: :
: : If typedef were to create a new type, what operations would be allowed
: : on the new type?
: :
: : I think that changing the meaning of `typedef' would be a bad idea.
: : It might make sense to propose a new construct, say `newtypedef', that
: : did something different, but C++ already has a way to create new types
: : (`class'), and it is not at all clear that adding a new way would be
: : worth the additional complexity.
: I see two issues at odds here, and a desire to satisfy them both with one
: construct.
: One issue is type safety in function calls. You might have
: typedef double DOLLARS;
: typedef double INTEREST_RATE;
: and want to define a function declared as
: DOLLARS PresentValue(DOLLARS futureValue,
: INTEREST_RATE rate, int YEARS);
: that will keep the user from accidentally swapping the money and interest
: arguments when coding a call to the function.
: On the other hand, within functions that use these types, you might want
: all the ordinary mathematical functions to apply as usual to the raw
: values underneath. The function call has already guaranteed that the set
: of arguments given has types that are meaningful given the function's
: purpose. Now we want to take these arguments for what they really
: are--doubles--and apply normal arithmetic to them with no headaches.
Wow, I did just exactly this just yesterday. I was having problems
keeping track of what numbers were in internal format, and which were
in external format, so I made two classes:
class externalValue {
double v;
public:
operator double() { return v; }
explicit externalValue(double x) { v = x; }
/* extra fns to read/write to files */
};
class internalValue {
double v;
public:
operator double() { return v; }
explicit internalValue(double x) { v = x; }
};
This way functions and classes can define variables as external or internal,
but complicated formulas involving the numbers still work fine.
I then have a series of conversion functions:
internalValue toInternal(externalValue v) {
return internalValue(3 * v - 4);
}
etc. Works quite nicely.
---------------------------------------------------------------------------
Tim Hollebeek | Disclaimer :=> Everything above is a true statement,
Electron Psychologist | for sufficiently false values of true.
Princeton University | email: tim@wfn-shop.princeton.edu
----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!)
---
[ To submit articles: Try just posting with your newsreader. If that fails,
use mailto:std-c++@ncar.ucar.edu
FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Technical Details
Message-ID:
<4gitti$ken@cnn.Princeton.EDU>Path:
arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austernReferences:
<4g5sm4$dtt@natasha.rmii.com> <4g8vdo$igt@mulga.cs.mu.OZ.AU> <4gimon$317@clarknet.clark.net>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern From: tim@franck.Princeton.EDU (Tim Hollebeek) Newsgroups: comp.std.c++ Subject: Re: typedef not strong Date: 22 Feb 1996 15:53:02 PST Organization: Princeton University Approved: austern@isolde.mti.sgi.com Message-ID: <4gitti$ken@cnn.Princeton.EDU> References: <4g5sm4$dtt@natasha.rmii.com> <4g8vdo$igt@mulga.cs.mu.OZ.AU> <4gimon$317@clarknet.clark.net> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 22 Feb 1996 23:23:30 GMT X-Newsreader: TIN [version 1.2 PL2] X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMS0B9Ey4NqrwXLNJAQFLewH+Mk82irh9uI9JSIPmC7yhg8nMZS5AQw2U nL6PYwP3HMxMOmD/AiPQIV1RfAatxRr52bPUVZdxWkxTpB4i1uh4YA== =kifU Originator: austern@isolde.mti.sgi.com