Ada:导入不等式运算符“/=”

Ada: Importing the inequality operator "/="

我不想 use 整个包,但我确实想导入一些功能,例如 "/=" 运算符。我知道 renames 允许我对大多数函数执行此操作,但是使用不等式运算符时我得到错误 explicit definition of inequality not allowed。如何在不引发错误的情况下导入此运算符?

package Integer_Maps is new Ada.Containers.Ordered_Maps
(
 Key_Type => Integer,
 Element_Type => Integer
);

-- the next line fails!
function "/=" ( Left, Right: Integer_Maps.Cursor ) return Boolean
   renames Integer_Maps."/=";

你没有!无论如何,不​​是直接的。重命名相等运算符 "=",您将免费获得不等式。

-- this line succeeds!
function "=" ( Left, Right: Integer_Maps.Cursor ) return Boolean
   renames Integer_Maps."=";

这类似于覆盖运算符。请参阅 ARM 6.6,特别是 静态语义

你可以做一个 use type Integer_Maps.Cursor; 来让运算符在类型上可见。

对于容器游标,做一个 use all type Integer_Maps.Cursor; 也可能是实用的,它提供了对类型的所有原始操作的可见性,比如 KeyElement

我通常将 use typeuse all type 子句放在需要它们的最内层封闭范围(即子程序内部)中,如下所示:

   procedure Foo is
       use all type Integer_Maps.Cursor; 
   begin
       for Cursor in My_Map.Iterate loop
          Ada.Text_IO.Put_Line
            (Integer'Image(Key(Cursor)) & " ->" & Integer'Image(Element(Cursor)));
       end loop;
   end Foo;