参数传递模式不一致

bac*_*ash 3 ada

考虑以下代码(使用GCC 4.7.4编译):

procedure Main is
    procedure Sub_Proc(N : in out Integer) is
        M : Integer;
    begin
        M := N;
    end;
    procedure Proc(N : out Integer) is
    begin
        Sub_Proc(N);
    end;

    N : Integer;
begin
    Proc(N);
end Main;
Run Code Online (Sandbox Code Playgroud)

该程序Proc应该确保N作为out参数的参数永远不会在他的身体内被读出.但它将此参数传递给一个Sub_Proc带有in参数的过程,因此可能会在此过程中读取前一个参数,而调用过程则确保相反.

它是GCC错误还是Ada标准特异性?

Sim*_*ght 7

你将收到一个Sub_Proc(N);电话警告:"N" may be referenced before it has a value.所以编译器试图帮助!

Ada 83中,你的程序本来是非法的:6.4.1(3)说"对于模式输出,变量不能是模式输出的正式参数".的确,使用-gnat83和经过重复的代码重排后允许它编译,相当于上面的警告是错误(Ada 83) illegal reading of out parameter.

Ada 95Ada 2012中,可以out在分配参数后读取参数值; 在ARM95 6.4.1(15)中,我们发现该值未初始化(如上面提到的警告消息所示),因此使用它是一个坏主意.

所以答案是,GNAT的行为符合标准.