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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,fc52c633190162e0 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!n59g2000hsh.googlegroups.com!not-for-mail From: "kevin cline" Newsgroups: comp.lang.ada Subject: Re: why learn C? Date: 1 Apr 2007 23:16:23 -0700 Organization: http://groups.google.com Message-ID: <1175494583.376672.93730@n59g2000hsh.googlegroups.com> 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> NNTP-Posting-Host: 76.186.24.40 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1175494584 31947 127.0.0.1 (2 Apr 2007 06:16:24 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 2 Apr 2007 06:16:24 +0000 (UTC) In-Reply-To: User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: n59g2000hsh.googlegroups.com; posting-host=76.186.24.40; posting-account=Thx6EwwAAAAirqf96i7UdETSL0vfyj5f Xref: g2news1.google.com comp.lang.ada:14721 Date: 2007-04-01T23:16:23-07:00 List-Id: 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. 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: 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)