错误消息:对“...”的前缀调用中的对象必须使用别名

Error message: Object in prefixed call to "..." must be aliased

我目前正在阅读这份文件:Ada for the C++ or Java Developer。第 8 章(第 33 页)介绍了 Ada 中的面向对象编程。本章以以下示例开头:

type T is tagged record
   V, W : Integer;
end record;

type T_Access is access all T;

function F (V : T) return Integer;

procedure P1 (V : access T);

procedure P2 (V : T_Access);

下页举例说明如何调用子程序P1:

declare
   V : T;
begin
   V.P1;
end;

这会导致以下错误:object in prefixed call to "P1" must be aliased (RM 4.1.3 (13 1/2))。如果我将 procedure P1 (V : access T); 替换为 procedure P1 (V : in out T);,则该示例已成功编译。这是文档中的错字吗?

ARM 4.1.3(13.1) was introduced in the 2005 revision in AI95-00252 and AI95-00407(我得到了印象,作为清理的一部分)。

我会说文件是错误的。也许material的这部分是在Ada 2005之前开发的。


的确,在某些情况下,标记的对象会自动别名:ARM 3.10(9)

[...] a formal parameter or generic formal object of a tagged type is defined to be aliased.

所以,这是合法的:

declare
   procedure Proc (Param : in out T) is
   begin
      Param.P1;
   end Proc;
   V : T;
begin
   Proc (V);
end;