comp.lang.ada
 help / color / mirror / Atom feed
From: Martin Krischik <krischik@users.sourceforge.net>
Subject: Re: prohibit certain generic instantiations in Ada 2005
Date: Sat, 11 Feb 2006 10:54:11 +0100
Date: 2006-02-11T10:54:11+01:00	[thread overview]
Message-ID: <2199832.3vxpToovbm@linux1.krischik.com> (raw)
In-Reply-To: 1139645084.563448.239040@g47g2000cwa.googlegroups.com

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 T>
> class My_Class{
> public:
> My_Class(){
>   T t;
>   Checker::check(t);
> 
> }
> };
> 
> int main(){
> 
>   My_Class<int> mc = My_Class<int>(); //COMPILE TIME ERROR!!!!
>   //The error comes from the fact that Checker::check(int) is private
>   My_Class<float> mc2 = My_Class<float>(); //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 <int T[]>
class My_Class{};

template <float T>
class My_Class{};

typedef double f(double x);
template <f* T>
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



  reply	other threads:[~2006-02-11  9:54 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-02-09 18:01 prohibit certain generic instantiations in Ada 2005 matteo.bordin
2006-02-09 20:02 ` Matthew Heaney
2006-02-10 14:18   ` matteo.bordin
2006-02-10 18:25     ` Dmitry A. Kazakov
2006-02-10 19:03       ` Georg Bauhaus
2006-02-11 10:45         ` Dmitry A. Kazakov
2006-02-11  8:04       ` matteo.bordin
2006-02-11  9:54         ` Martin Krischik [this message]
2006-02-11 10:20           ` matteo.bordin
2006-02-11 12:46             ` Martin Krischik
2006-02-11 10:45         ` Dmitry A. Kazakov
2006-02-11 22:20           ` Jeffrey R. Carter
2006-02-11 15:36         ` Stephen Leake
2006-02-12  8:55           ` matteo.bordin
2006-02-12  9:49             ` Dmitry A. Kazakov
2006-02-12 12:41               ` matteo.bordin
2006-02-13 19:14             ` Matthew Heaney
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox