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-Thread: 103376,635cd9622b25ae59 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!y43g2000cwc.googlegroups.com!not-for-mail From: "REH" Newsgroups: comp.lang.ada Subject: Re: Type safety, C++ and code generation Date: 27 Apr 2006 09:05:04 -0700 Organization: http://groups.google.com Message-ID: <1146153904.898288.238600@y43g2000cwc.googlegroups.com> References: <1146143954.169807.207080@t31g2000cwb.googlegroups.com> <1146148380.102042.119860@y43g2000cwc.googlegroups.com> NNTP-Posting-Host: 192.35.35.34 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1146153910 13130 127.0.0.1 (27 Apr 2006 16:05:10 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 27 Apr 2006 16:05:10 +0000 (UTC) In-Reply-To: User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: y43g2000cwc.googlegroups.com; posting-host=192.35.35.34; posting-account=lnUIyw0AAACoRB2fMF2SFTIilm8F10q2 Xref: g2news2.google.com comp.lang.ada:3962 Date: 2006-04-27T09:05:04-07:00 List-Id: Maciej Sobczak wrote: > REH wrote: > > > I don't see where you've "done that." > > The template class that implements range checking? NO, a class that uses template to ELIMINATE unnecessary checks. > What about making different types really distinct? > > typedef ranged_type R1; > typedef ranged_type R2; > typedef ranged_type R3; My code uses this technique if you want truely unqiue types: class R1_unique{}; class R2_unique{}; typedef ranged_type R1; typedef ranged_type R2; > > Another problem is that the range, as a set of values, is not the only > thing that constitues a type - you also need to define the set of legal > operations. The problem is that I usually want them to be different in > each type. > Consider this: > > type ranged_type Speed; > Speed s1, s2, s3; // with some values > s1 = s2 + s3; // OK > s1 = s2 * s3; // not OK > > The addition is fine, but the multiplication should not be provided, > because speed multiplied by speed is not a speed. Can you extend your > class so that the compiler will refuse to compile the second operation > above? Yes, you derive from the class and put the multiplication operator in the private scope. > Another problem is variation of the behaviour in the out-of-range > condition. What should happen then? Throw an exception? That's only one > of at least four different options I can imagine, and also not the one I > would choose most of the time. Does your class allow variations here? Yes, the template takes a traits class. If allow modification of various behaviors, such as what should be done with an out-of-range value, an overflow condition, a divide-by-zero, etc. > > And so on - all this *can* be done with templates, but at some point > this approach just saturates and the code becomes a nightmare. Not > mentioning the error messages that you get from the compiler (and the > whole purpose of this is to actually get *useful* error messages, > right?). And not mentioning the compilation time. I've heard all these things. That may have been true with older compilers. They are a lot better today. REH