'访问属性不允许在具有外部包的通用正文中



我在 Ada 中的泛型方面遇到了一些问题。给定以下示例,gnatmake 结果为:

g_package.adb:13:33: 'Access attribute not allowed in generic body
g_package.adb:13:33: because access type "t_callback_p" is declared outside generic unit (RM 3.10.2(32))
g_package.adb:13:33: move 'Access to private part, or (Ada 2005) use anonymous access type instead of "t_callback_p"
gnatmake: "g_package.adb" compilation error

假设外部包无法更改,有没有办法解决这个问题?我首先知道错误消息的原因(编译器不知道通用包的正确类型,但是当有问题的传递函数不接触泛型的任何部分时,这有点烦人。

g_package.亚行

with Ada.Text_IO; use Ada.Text_IO;
with external;
package body g_package is
   procedure quix (f : String) is
   begin
      Put_Line ("Does a thing");
   end quix;
   procedure foo (bar : String) is
   begin
      Put_Line ("baz" & bar & Boolean'Image(flag));
      external.procedure_f (quix'Access);
   end foo;
end g_package;

g_package.广告

generic
   flag : Boolean;
package g_package is
   procedure foo (bar : String);
end g_package;

外部广告

package external is
   type t_callback_p is access procedure (s : String);
   procedure procedure_f (proc : t_callback_p);
end external;

您可以(如错误消息中所述(将"访问"移动到软件包规范的私有部分:

private with external;
generic
   flag : Boolean;
package g_package is
   procedure foo (bar : String);
private
   procedure quix (f : String);
   quix_access : constant external.t_callback_p := quix'Access;
end g_package;

并在体内使用常数:

external.procedure_f (quix_access);

在 Ada 中没有办法做到这一点。如果您希望不可移植并且正在使用(看起来(GNAT,则可以使用 GNAT 特定的属性'Unrestricted_Access

最新更新