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,262b74f44c7f873e X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news3.google.com!news2.volia.net!newsfeed01.sul.t-online.de!t-online.de!news.belwue.de!kanaga.switch.ch!news-zh.switch.ch!switch.ch!news.hispeed.ch!linux2.krischik.com!news From: Martin Krischik Newsgroups: comp.lang.ada Subject: Re: prohibit certain generic instantiations in Ada 2005 Date: Sat, 11 Feb 2006 10:54:11 +0100 Organization: Cablecom Newsserver Message-ID: <2199832.3vxpToovbm@linux1.krischik.com> References: <1139508110.410006.28260@g14g2000cwa.googlegroups.com> <1139515341.782860.197930@f14g2000cwb.googlegroups.com> <1139581110.636535.107910@g44g2000cwa.googlegroups.com> <1139645084.563448.239040@g47g2000cwa.googlegroups.com> NNTP-Posting-Host: 84-74-134-212.dclient.hispeed.ch Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.hispeed.ch 1139652932 15001 84.74.134.212 (11 Feb 2006 10:15:32 GMT) X-Complaints-To: news@hispeed.ch NNTP-Posting-Date: Sat, 11 Feb 2006 10:15:32 +0000 (UTC) User-Agent: KNode/0.10.1 Xref: g2news1.google.com comp.lang.ada:2856 Date: 2006-02-11T10:54:11+01:00 List-Id: matteo.bordin@gmail.com wrote: > I will provide a proper example in C++. With this code I can prohibit > instantiation of template My_Class with T=int. > > class Checker{ > public: > static void check(float){}; > private: > static void check(int){}; > }; > > template > class My_Class{ > public: > My_Class(){ > T t; > Checker::check(t); > > } > }; > > int main(){ > > My_Class mc = My_Class(); //COMPILE TIME ERROR!!!! > //The error comes from the fact that Checker::check(int) is private > My_Class mc2 = My_Class(); //OK!!! > }; Sorry, but that look like a hack to me. A hack to hack in a feature into a language which does not have that feature. > When I said that the compilation model for template of Ada is similar > to C++, I meant in regards to compile-time code generation (opposed to > the model of Java generics). In both languages (Ada and C++), templates > can be used as a generative environment. > I just want to know f here is a way to reproduce the C++ code I > provided in Ada (2005). The obvious solution would be: generic type T is digits <>; package My_Class end My_Class; Of course that is positive logic: Instead of disallowing integer I only allow floating point types. But that is the natural way in Ada: you declare which types are suitable for the template. I don't think you can compare C++ and Ada on that level. C++ only has user defined struct/union/class. And as such only struct/union/class play any important role in templates all other types are only 2nd class. Or can you any of those: template class My_Class{}; template class My_Class{}; typedef double f(double x); template class My_Class{}; Yes you could do then all with the aid of a helper class. But that would only proof my point: For templates struct/union/class in 1st class all other only 2nd class after thoughts. And you initial example needed a helper class as well. Ada on the other had has user defined integer, floats, decimals, arrays, functions and record (did I foreget one) and so you can specify any of those as parameter to a tempate. Ada supports all the examples above without a helper class or package: generic type Integer_Type is range <>; -- optional type Array_Type is array (Positive range <>) of Integer_Type; -- optional T : in Array_Type; package My_Class end My_Class; generic type Float_Type is digits <>; -- optional T : in Float_Type; package My_Class end My_Class; generic type Float_Type is digits <>; -- optional with function T (x : in Float_Type) return Float_Type; package My_Class end My_Class; As you see: an different concept alltogether. Martin -- mailto://krischik@users.sourceforge.net Ada programming at: http://ada.krischik.com