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