Question about vector initialization
Yesterday, I was trying to do something with an array of vectors and stumbled a bit. So Iβd like to ask some clarification questions about initializationβ¦
With arrays (as with most other types), a variable is uninitialized until itβs explicitly set to a value.
My_List : array (1 .. 10) of Boolean; -- uninitialized
My_List := [1 => True, other => False];
With vectors itβs slightly different. The reference manual says:
If an object of type Vector is not otherwise initialized, it is initialized to the same value as Empty_Vector.
So if I have:
package Boolean_Vectors is Ada.Containers.Vectors (
Index_Type => Positive,
Element_Type => Boolean
);
subtype Boolean_Vector is Boolean_Vectors.Vector;
I can actually do:
My_Vector : Boolean_Vector; -- Implicitly initialized here?
My_Vector.Append (True); -- Or here?
My_Vector.Append (False);
Although Iβm not sure when exactly initialization happens (and why it was designed to happen implicitly).
Now, Empty_Vector
is a constant, so I cannot use it if I want to initialize an empty vector explicitly.
So, what do I do when I want to have an array of vectors?
My_Vector_List : array (1 .. 10) of Boolean_Vector;
My_Vector_List := [others => ??]; -- How do I put an empty vector here
-- that can grow later?
Or do I have to do it like this?
My_Vector_1, My_Vector_2 : Boolean_Vector;
My_Vector_List (1) := My_Vector_1;
My_Vector_List (2) := My_Vector_2;
...
9 posts - 7 participants