comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: prohibit certain generic instantiations in Ada 2005
Date: Sat, 11 Feb 2006 11:45:42 +0100
Date: 2006-02-11T11:45:31+01:00	[thread overview]
Message-ID: <1cfq4cp2jypst$.q1w29pgsumem$.dlg@40tude.net> (raw)
In-Reply-To: 1139645084.563448.239040@g47g2000cwa.googlegroups.com

On 11 Feb 2006 00:04:44 -0800, 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!!!
> };

Your example in Ada would be:

generic
   type T is digits <>; -- Only floating-point is allowed

T is specified by its contract: a floating-point type, rather than using
ad-hoc matching.

It isn't clear what you are going to achieve using the technique like in
your example. What if T were long?

The trick you mention should be impossible in legal Ada, because generics
are compilable separately [thank to the contract model.] So if any error
may occur, then only in the formal part. [GNAT Ada has a lot of problems
with this, but these are just bugs.] In means that any trick should rely on
the formal parameter part. For example:

generic
   type T is private;
   with function "+" (Left : T) return Float is <>;

This will reject Integer and accept Float. But, again, it is an awful
style.

Now to your example. It has a direct Ada equivalent (checks are moved to
the formal part):

package Checker is
   function Check (T : Float) return Float renames "+";
end Checker;

with Checker;  use Checker;
generic
   type T is private;
   wth function Check (X : T) return T is <>;

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



  parent reply	other threads:[~2006-02-11 10:45 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
2006-02-11 10:20           ` matteo.bordin
2006-02-11 12:46             ` Martin Krischik
2006-02-11 10:45         ` Dmitry A. Kazakov [this message]
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