From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,1dd28d5040ded1f8 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-05-15 01:16:27 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!fu-berlin.de!uni-berlin.de!pec-14-111.tnt3.hh2.uunet.DE!not-for-mail From: Dmitry A. Kazakov Newsgroups: comp.lang.ada Subject: Re: Announce: Grace project site operational Date: Wed, 15 May 2002 10:17:52 +0200 Message-ID: <2s44eu0fm4g6606h9p4stb1b5oc0nmg5u8@4ax.com> References: <4519e058.0205140718.3ddb1b2a@posting.google.com> <3CE15D0A.3050100@mail.com> NNTP-Posting-Host: pec-14-111.tnt3.hh2.uunet.de (149.225.14.111) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: fu-berlin.de 1021450586 22303771 149.225.14.111 (16 [77047]) X-Newsreader: Forte Agent 1.8/32.548 Xref: archiver1.google.com comp.lang.ada:24075 Date: 2002-05-15T10:17:52+02:00 List-Id: On Tue, 14 May 2002 14:52:58 -0400, Hyman Rosen wrote: >Time for me to bring up my usual post about units in C++, >which can be done with complete type safety and with no >run-time overhead in time or storage, because of C++'s >automatic instantiation rules. The same could be achieved without templates if there were a way to ensure that a user-defined subroutine is compile-time and known type discriminants / tags are removed [*]. I believe that an improvement of Ada's ADT/OO gears is much more important than any investments in inherently flawed generics-templates-macros. Generally, I dislike any type-based solution of the unit problem. I believe that the right way is a subtype-based, OO-ish one. Whether the dimension be a discriminant or tag [**] is no matter. There must be a way to write class-wide subroutines for dimensioned values. Any type-based solution lacks that without any hope. [*] Well, there is a performance problem with tagged types which were unnecessarily forced to be always by-reference. [**] Here I consider derived tagged types "subtypes". >Here's the simplified >example involving only mass, distance, and time. The code >allows only like units to be added or subtracted, but >arbitrary units to be multiplied or divided, with the >correctly typed result. Constants are units with all >paremeters set to zero. All the verbiage is for types; >the actual storage for each unit object is just the >value, and all type correctness is checked at compile >time, not run time. The correctly typed operators are >automatically instantiated when used, without manual >intervention. > >I first saw this idiom in _Scientific and Engineering C++_ >by Bartion & Nackman. > >template >struct Unit >{ > RepType value; > explicit Unit(value) : value(value) { } A small correction. The above should be: explicit Unit(RepType value) : value(value) {} > Unit operator+(Unit other) > { return Unit(value + other.value); } > Unit operator-(Unit other) > { return Unit(value - other.value); } >}; > >template int MassL, int DistanceL, int TimeL, > int MassR, int DistanceR, int TimeR> >Unit MassL + MassR, > DistanceL + DistanceR, > TimeL + TimeR> >operator*(Unit l, > Unit r) >{ > return Unit MassL + MassR, > DistanceL + DistanceR, > TimeL + TimeR>(l.value * r.value); >} > >template int MassL, int DistanceL, int TimeL, > int MassR, int DistanceR, int TimeR> >Unit MassL - MassR, > DistanceL - DistanceR, > TimeL - TimeR> >operator/(Unit l, > Unit r) >{ > return Unit MassL - MassR, > DistanceL - DistanceR, > TimeL - TimeR>(l.value / r.value); >} > >typedef Unit Velocity; >typedef Unit Acceleration; >typedef Unit Energy; >typedef Unit Herz; >// etc. As always with C++, the above does not work with MS-VC++, but it does work with gcc. Diagnostics in case of a unit error is a mess, as expected. (:-)) --- Regards, Dmitry Kazakov www.dmitry-kazakov.de