comp.lang.ada
 help / color / mirror / Atom feed
From: "Jean-Pierre Rosen" <rosen.adalog@wanadoo.fr>
Subject: Re: Types vs subtypes
Date: 2000/08/29
Date: 2000-08-29T13:58:46+00:00	[thread overview]
Message-ID: <8ogfim$e2t$1@wanadoo.fr> (raw)
In-Reply-To: 39ABAAE7.3B4BEF29@lmco.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2186 bytes --]


"Marc A. Criley" <marc.a.criley@lmco.com> a �crit dans le message news: 39ABAAE7.3B4BEF29@lmco.com...
> We had a situation where there was a
>
>    type Counts is range 0..128;
>
> that had a size clause:
>
>    for Counts'Size use 32;
>
> This then had a subtype
>
>    subtype Indicies is Counts range 1..128;
>
> Our expectation was that Counts'Size would be 32, and Indices'Size would be 32.
> While that was in fact the case for the 'Size of Counts, Indices'Size turned out to
> be 8.  We researched this in the RM (and could find no statement requiring that a
> type's size be passed on to its subtypes), and also went back to our compiler
> vendor, and confirmed that this behavior was correct.  It was, as Robert Dewar put
> it, "in pure Ada 95...a nasty omission".  While size specification is retained
> through derivation, it is not through subtyping.  And placing a Size specification
> on a subtype is not permitted by the language.
>
> We ended up utilizing the parent type in the external interface, relying on the use
> of the subytpe throughout the remainder of the module to ensure the proper
> constraints were obeyed.
It seems that you assumed that *variables* declared of subtype Indices would use Indices'Size bits - and this is a misunderstanding.
Try this:

with Text_Io; use Text_Io;
procedure Essai is
   type Counts is range 0..128;
   for Counts'Size use 32;
   subtype Indices is Counts range 1..128;
   V_Counts  : Counts;
   V_Indices : Indices;
begin
   Put_Line ("Sizes:");
   Put_Line ("Counts:" & Integer'Image (Counts'Size));
   Put_Line ("Indices:" & Integer'Image (Indices'Size));
   Put_Line ("V_Counts:" & Integer'Image (V_Counts'Size));
   Put_Line ("V_Indices:" & Integer'Image (V_Indices'Size));
end Essai;

You'll see (with Gnat) that V_Indices has size 32.
In short: the 'Size for a (sub)type tells you the minimum number of bits required for it according to Shannon's theorem. If you want
to check the size used by a variable, declare a variable and take its 'size.

--
---------------------------------------------------------
           J-P. Rosen (Rosen.Adalog@wanadoo.fr)
Visit Adalog's web site at http://pro.wanadoo.fr/adalog






  reply	other threads:[~2000-08-29  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-08-28  2:47 Types vs subtypes Alex Angas
2000-08-28  0:00 ` Richard Riehle
2000-08-29  0:00   ` Marc A. Criley
2000-08-29  0:00     ` Jean-Pierre Rosen [this message]
2000-08-29  6:06   ` Preben Randhol
replies disabled

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