comp.lang.ada
 help / color / mirror / Atom feed
From: "Peter C. Chapin" <pchapin@sover.net>
Subject: Re: Question about generics.
Date: 03 Jul 2006 10:33:52 GMT
Date: 2006-07-03T10:33:52+00:00	[thread overview]
Message-ID: <Xns97F542D31B46Apchapinsovernet@198.186.192.137> (raw)
In-Reply-To: 7Q2qg.812592$084.507058@attbi_s22

"Jeffrey R. Carter" <spam.not.jrcarter@acm.not.spam.org> wrote in
news:7Q2qg.812592$084.507058@attbi_s22: 

> Peter C. Chapin wrote:
>> 
>> generic
>>   Size : in Integer;
>> package Xyzzy is
>>   type Index is mod Size;
>>   -- etc.
>> end Xyzzy;
> 
> What does a Size of -7 mean?

Why not a compile time error when the instantiation is attempted?

> As you've discovered, generic formal parameters are not static in Ada,
> and so cannot be used in a type declaration.
> 
> You can do
> 
> generic
>     type Index is mod <>;
> package P is
>     Size : constant Positive := Index'Modulus;
> end P;

Yes. However, this means at the point of instantiation I have to do 
something like

    	type Dummy_Type is mod 8;
    	package Fizzle is new Xyzzy(Index => Dummy_Type);

The Dummy_Type will never be used in the context of the instantiation; 
it only exists to pass size information into the generic package. That 
seems pretty counter-intuitive and awkward. I guess I was hoping Ada 
would have a nicer solution... generic parameters that are named numbers 
or something.

>> template< int size >
>> class Xyzzy {
>>   char array[size];  // size is a compile time constant.
>> };
> 
> This is a different kettle of fish. You can't declare new integer
> types in C++, so you can't compare what you're trying to do with C++.
> Array subtype constraints don't have to be static, or even constant:

Don't be distracted by the fact that I used an array declaration above 
to demonstrate the "static-ness" of size. I realize that array bounds 
don't have to be static in Ada. However, my point was that C++ templates 
do allow non-type parameters to be used in contexts where compile time 
constants are required.

For my particular situation I believe I could declare a subtype inside 
the generic package instead of a modular type. Alas, that means I'll 
have to manually wrap around values of my type. It's not the end of the 
world but, as I said, I was hoping for a nicer solution.

Peter



  reply	other threads:[~2006-07-03 10:33 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-02 16:08 Question about generics Peter C. Chapin
2006-07-02 18:49 ` Martin Krischik
2006-07-03  6:30 ` Jeffrey R. Carter
2006-07-03 10:33   ` Peter C. Chapin [this message]
2006-07-03 11:42     ` Jean-Pierre Rosen
2006-07-03 16:44     ` Pascal Obry
2006-07-04  1:09       ` Peter C. Chapin
2006-07-04  6:17         ` M E Leypold
2006-07-04 10:48           ` Peter C. Chapin
2006-07-03 20:03     ` Jeffrey R. Carter
2006-07-03 20:18       ` Dmitry A. Kazakov
2006-07-04  0:08         ` Randy Brukardt
2006-07-04  7:48           ` Dmitry A. Kazakov
2006-07-04  0:43         ` Jeffrey R. Carter
2006-07-03  9:46 ` Martin Krischik
2006-07-04 13:29 ` Stephen Leake
2006-07-05 12:08   ` Dmitry A. Kazakov
  -- strict thread matches above, loose matches on Subject: below --
1993-09-02 17:45 question " Stef  Van Vlierberghe
1993-08-12 15:18 Robert I. Eachus
1993-08-11 18:48 cis.ohio-state.edu!magnus.acs.ohio-state.edu!math.ohio-state.edu!howland.
1993-08-11  0:25 agate!howland.reston.ans.net!europa.eng.gtefsd.com!darwin.sura.net!seas.g
1993-08-10 15:53 Robert I. Eachus
1993-08-09 21:29 Kenneth Anderson
1989-05-29 20:54 Question " "14827_DAVID PAPAY"
1989-05-29  7:02 "Jonathan B. Owen"
replies disabled

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