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!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!t-online.de!130.59.10.21.MISMATCH!kanaga.switch.ch!ezmp3.switch.ch!news-zh.switch.ch!switch.ch!cernne03.cern.ch!cern.ch!news From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Re: Type safety, C++ and code generation Date: Thu, 27 Apr 2006 17:16:45 +0200 Organization: CERN - European Laboratory for Particle Physics Message-ID: References: <1146143954.169807.207080@t31g2000cwb.googlegroups.com> <1146148380.102042.119860@y43g2000cwc.googlegroups.com> NNTP-Posting-Host: abpc10883.cern.ch Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sunnews.cern.ch 1146151005 23587 (None) 137.138.37.241 X-Complaints-To: news@sunnews.cern.ch User-Agent: Mozilla Thunderbird 1.0.8-1.4.1.SL (X11/20060424) X-Accept-Language: en-us, en In-Reply-To: <1146148380.102042.119860@y43g2000cwc.googlegroups.com> Xref: g2news2.google.com comp.lang.ada:3961 Date: 2006-04-27T17:16:45+02:00 List-Id: REH wrote: >>>>http://www.msobczak.com/prog/typegen/ >> >>>You maybe interested in a C++ class that I wrote. >> >>Been there, done that: >> >>http://www.msobczak.com/prog/downloads.html >> >>(see safetypes.tar.gz and range.tar.gz) > > > I don't see where you've "done that." The template class that implements range checking? >>I have dropped this idea because I think that the external code >>generator is much more flexible and more powerful with regard to the >>type safety that can be gained. Range checking is just one little part >>of what is needed. Template classes are fine if you can afford limiting >>yourself only to this little part, but that's not usually the case. > > I never said it was the end-all-be-all, just thought you may be > interested. I don't know what you think templates are limiting you to. It's not about templates themselves, but rather about the whole approach. What about making different types really distinct? typedef ranged_type R1; typedef ranged_type R2; typedef ranged_type R3; Above, R1 and R2 are *equal* to the compiler, but R3 is distinct from the other two. This is or can be problematic in those contexts where you would rather expect them all to be different from each other - especially when you think in terms of different *domains*, no matter what is their range of values. 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? (Ada, anyone? :) ) 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? (Ada, anyone? :) ) 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. These are exactly the factors that limit the practical useability of the template approach and that's why I claim that external code generator can be much more powerful. -- Maciej Sobczak : http://www.msobczak.com/ Programming : http://www.msobczak.com/prog/