0
我想使用聚合初始化一个简单的Ada数组,并且我希望编译器确定数组边界。但是,当尝试使用下面的Test_2时,我不能简单地使用整数下标。有没有办法允许编译器确定数组边界数组,然后使用简单的“Test_2(0)”符号访问它们?Ada数组聚合初始化
我正在使用gnat。
谢谢。
with Interfaces; use Interfaces;
procedure Test_Init is
type U16_a is array(Integer range <>) of Unsigned_16;
-- array aggregate initialization
Test_1 : U16_a(0..1) := (16#1234#, 16#5678#); -- ok, but...
Test_2 : U16_a := (16#8765#, 16#4321#); -- let compiler create bounds
Test_3 : Unsigned_16;
begin
-- Test_1 obviously works
Test_3 := Test_1(0);
-- warning: value not in range of subtype of "Standard.Integer" defined at line 8
-- This produces a constraint.
-- What is the subtype that is defined at line 8? It is not Integer (0..1)
Test_3 := Test_2(0);
-- this works though
Test_3 := Test_2(Test_2'First);
-- and this works
Test_3 := Test_2(Test_2'Last);
-- and this works
Test_3 := Test_2(Test_2'First + 1);
end Test_Init;
或指定聚集中的边界,如下所示:'Test_2:U16_a:=(0 => 16#8765#,1 => 16#4321#);' – egilhh
@egilhh:是的,但那不规模也是如此。如果您有大量元素,则必须为每个元素指定索引,因为位置关联不能跟随命名关联。 –
当然,但你可能会从文件中读取数组元素 – egilhh