Ada 在数组上使用方面 Default_Component_Value 和 Pragma Pack

jul*_*ius 5 ada

我正在使用 gnat gcc 11.1,想知道是否有人可以向我解释这种行为:

这是我的代码:

with Ada.Text_IO;           use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
pragma Ada2012;

procedure Hello is
   type bool_arr is array (Integer range 1 .. Integer'Size) of Boolean with
      Default_Component_Value => True;
   pragma Pack (bool_arr);

   test : bool_arr;

   procedure P is
      idx   : String           := "index ";
      strg  : Unbounded_String := To_Unbounded_String (idx);
      strg2 : Unbounded_String;
   begin
      for I in test'range loop
         Append (strg, I'Image);
         Append (strg2, " " & test (I)'Image);
      end loop;
      Put_Line (To_string (strg));
      Put_Line (To_String (strg2));

   end P;
begin
   P;
end Hello;
Run Code Online (Sandbox Code Playgroud)

作为输出我得到这个:

你好世界!索引 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 TRUE TRUE FALSE FALSE FALSE TRUE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FAL SE 假假假假假假假假假假假假假假假假假假假假假假假假

通过设置,这不是我想要的with Default_Component_Value => True

如果我注释掉Pragma Pack(bool_arr),那么我会得到:

你好世界!索引 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE真实真实真实真实真实真实真实真实真实真实真实真实真实真实真实真实

谢谢您的帮助。

Nik*_*sti 5

很抱歉,看起来像是 GNAT 中的一个错误。

如果我通过声明显式使用 Default_Component_Value

   test : bool_arr := (others => <>);
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,所有组件都是 True。