You can say:
package Some_Vector is new Ada.Containers.Indefinite_Vectors (
Index_Type => Positive,
Element_Type => T'Class
);
Some_Vector
is now able to hold elements of type T
or any type derived from it, including A
or B
. There's no requirement that all the elements have to be of the same type, as long as they're all derived from T
; so if you don't really care whether this property is enforced, the above should work. If you really want the compiler to enforce that all elements are the same type, then you should simply declare two packages, A_Vector
and B_Vector
, for vectors of the two types; but then there's no way to write a "class-wide" type name that could refer to either an A_Vector
or a B_Vector
.
If you really want to combine both--have a vector type that could refer either to a vector of A
or a vector of B
, but still enforce that all elements of the vector have the same type, then I think this could be done if you define your own vector type and perform the needed check at run time, but it could get complicated. This compiles, but I haven't tested it:
generic
type Elem is new T with private;
package Sub_Vectors is
type Sub_Vector is new Some_Vector.Vector with null record;
overriding
procedure Insert (Container : in out Sub_Vector;
Before : in Some_Vector.Extended_Index;
New_Item : in T'Class;
Count : in Ada.Containers.Count_Type := 1)
with Pre => New_Item in Elem;
end Sub_Vectors;
package body Sub_Vectors is
procedure Insert (Container : in out Sub_Vector;
Before : in Some_Vector.Extended_Index;
New_Item : in T'Class;
Count : in Ada.Containers.Count_Type := 1) is
begin
Some_Vector.Insert
(Some_Vector.Vector(Container), Before, New_Item, Count);
end Insert;
end Sub_Vectors;
Unfortunately, you'd have to override every Insert
and Replace_Element
operation that could put an element into the vector. After you do all this, though, you can instantiate Sub_Vectors
with Elem => A
and with Elem => B
, and the class Some_Vector.Vector'Class
would be a class-wide type that would include both Sub_Vector
types in the instance packages.