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,fc52c633190162e0 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Newsgroups: comp.lang.ada Subject: Re: why learn C? References: <1172144043.746296.44680@m58g2000cwm.googlegroups.com> <1172161751.573558.24140@h3g2000cwc.googlegroups.com> <546qkhF1tr7dtU1@mid.individual.net> <5ZULh.48$YL5.40@newssvr29.news.prodigy.net> <1175215906.645110.217810@e65g2000hsc.googlegroups.com> <1175230700.925143.28490@n59g2000hsh.googlegroups.com> <1175494583.376672.93730@n59g2000hsh.googlegroups.com> From: Markus E Leypold Organization: N/A Date: Tue, 03 Apr 2007 02:13:38 +0200 Message-ID: <6ur6r2iabx.fsf@hod.lan.m-e-leypold.de> User-Agent: Some cool user agent (SCUG) Cancel-Lock: sha1:zqGDJ8JIL/2+fHDonZdMMWWxCmI= MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii NNTP-Posting-Host: 88.72.209.234 X-Trace: news.arcor-ip.de 1175558794 88.72.209.234 (3 Apr 2007 02:06:34 +0200) X-Complaints-To: abuse@arcor-ip.de Path: g2news1.google.com!news1.google.com!proxad.net!212.101.4.254.MISMATCH!solnet.ch!solnet.ch!newsfeed.arcor-ip.de!news.arcor-ip.de!not-for-mail Xref: g2news1.google.com comp.lang.ada:14777 Date: 2007-04-03T02:13:38+02:00 List-Id: "kevin cline" writes: > On Apr 2, 12:03 am, Brian May wrote: >> >>>>> "kevin" == kevin cline writes: >> >> kevin> "The big difference is that C++ templates allow type >> kevin> checking during compile-time [...] >> >> How is this any different from Ada generics? > > Because Ada generics have to be explicitly instantiated. That doesn't make them non type checking. Quite the opposite. And as far as type checking in templates goes: Last time I looked, many usage errors intemplates where not caught by a kind of type systems (which would say "you can't use this tape as a parameter in a template), but rather by the linker wwhich told me that there is no usch function/method for this type: This is not type checking. > Ada requires > an explicit instantiation for every combination of types that appears > in the program. C++ template functions are implicitly instantiated, > and can be defined once to cover all combinations of physical units, > like this: I think we had the problem already once: What about floating point parameters to templates? :-) > > template class Unit > // Mechanical unit with mass dimension M, length dimension L, > // and time dimension T. > { > private: > double value; > public: > explicit Unit(double v): value(v) {} > Unit operator+(Unit rhs) { return Unit(v + rhs.v); } > Unit operator-(Unit rhs) { return Unit(v - rhs.v); } > Unit operator*(Unit& rhs) > { return Unit(v * rhs.v); } > ... > }; > > typedef Unit<0,0,0> Scalar; > typedef Unit<1,0,0> Mass; > typedef Unit<0,1,0> Length; > typedef Unit<0,0,1> Time; > typedef Unit<1,1,-2> Force; > typedef Unit<1,-1,-2> Pressure; > typedef Unit<1,-3,0> Density; > > Mass gram(1); > Length meter(1); > Time second(1); > > Pressure water_pressure(Length head) > { > Force g_earth = Scalar(9.8)*meter/second/second; > Density water_density = Scalar(1000000)*gram/meter/meter/meter; > return head * g_earth * water_density; > } > > Ada would require a separate instantiations for each of these > operations: > > Scalar * Length (returning Length) > Length / Time (returning Velocity) > Velocity / Time (returning Acceleration) > Scalar * Mass (returning Mass) > Mass / Length (returning Unit<1,-1,0>) > Unit<1,-1,0>/Length (returning Unit<1,-2,0>) > Unit<1,-2,0>/Length (returning Density) > Length * Acceleration (returning Unit<0,-2,2>) > Unit<0,2,2>*Density (returning Pressure) Who cares? I'm a physicist (by education), but I have come to the conclusion that judging the merits of a programming system on how much I have to type to support checking physical units is cranky: It's a fringe application and I can write a "physical units" definition file and a translator to translate this into a Physical_Units package (which does not only contain kg*m/s^2 (as your template does) but actually the type Force) in some hours. BTW: I haven't checked your C++ example, but I'd expect trouble if one calculates f / m * t1 * t2 as opposed to t1 * t2 * f / m since you need (dependend on the implicit grouping/association of the multiplication operator) also operations time * time -> time^2 time * force -> momemntum [kg*m/s] and so on. Maybe the templating would automatically provide all this (more power to C++ then), but it certainly looks like a complete mess Regards -- Markus