comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Question Exception with Ada.Containers.Generic_Constrained_Array_Sort
Date: Mon, 13 Aug 2012 14:25:47 -0700 (PDT)
Date: 2012-08-13T14:25:47-07:00	[thread overview]
Message-ID: <10db9c3d-28c2-4b42-8cd1-4b8f5d86d6ed@googlegroups.com> (raw)
In-Reply-To: <097437ed-1197-423f-8c00-4480fb2f8ebb@googlegroups.com>

On Monday, August 13, 2012 1:44:19 PM UTC-7, awdorrin wrote:
> I managed to find a way to get this to work, but I am really not sure I'm doing this the right way. :)
> 
> 
> 
> First, I changed from using Generic_Constrained_Array_Sort to the unconstrained version.
> 
> 
> 
> Second, I changed my declarations like this:
> 
> 
> 
> type Device_Array_Type is array( Device_Index_Type range <> ) of Device_Object_Type; // make this unconstrained
> 
> 
> 
> type DeviceListType is record
> 
>   Num_Devices : Device_Index_Type;
> 
>   DeviceList  : Device_Array_Type(Device_Index_Type); // limit range 1..128
> 
> end record;
> 
> 
> 
> Code appears to do what I want it to, but I'm really not sure this is the right solution.

Well, Generic_Constrained_Array_Sort is declared like this:

generic
   type Index_Type is (<>);
   type Element_Type is private;
   type Array_Type is array (Index_Type) of Element_Type;
   with function "<" (Left, Right : Element_Type)
      return Boolean is <>;
procedure Ada.Containers.Generic_Constrained_Array_Sort
      (Container : in out Array_Type);

In your first example, the actual for Array_Type is Device_Array_Type, which is defined as an array with bounds 1..128.  So the generic instantiation declares a function DXSort which takes an array parameter of type Device_Array_Type, a constrained array with bounds 1..128.  You're passing it a parameter with fewer elements than that, so you'll get a Constraint_Error when you try to pass the parameter.

Since you don't know beforehand what size array you're passing to your sort, you need to create a sort function that takes an unconstrained array parameter.  Which is what you did in your second attempt.

So, yes, you did find the right solution.  But if you still want to have a "Device_Array_Type" that is constrained, you can do that with a subtype:

type Device_Array_Type_Unc is array (Device_Index_Type range <>) 
    of Device_Object_Type;
subtype Device_Array_Type is Device_Array_Type_Unc(Device_Index_Type);

Now you can still say

  DeviceList: Device_Array_Type;

Just make sure you use the *unconstrained* version (Device_Array_Type_Unc) when instantiating Generic_Array_Sort.

                        -- Adam




  reply	other threads:[~2012-08-13 21:27 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-13 20:05 Question Exception with Ada.Containers.Generic_Constrained_Array_Sort awdorrin
2012-08-13 20:44 ` awdorrin
2012-08-13 21:25   ` Adam Beneschan [this message]
2012-08-14 12:22     ` awdorrin
2012-08-14 12:39 ` Marc C
2012-08-15  7:11   ` Martin
2012-08-15 16:02     ` Georg Bauhaus
replies disabled

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