我不想使用
整个包,但我确实想导入一些功能,例如 "/="
运算符。我知道 renames
允许我对大多数函数执行此操作,但是使用不等式运算符我得到错误,不允许显式定义不等式
。如何在不引发错误的情况下导入此运算符?
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."/=";
最佳答案
你可以做一个 use type Integer_Maps.Cursor;
来提供类型上的操作符的可见性。
对于容器游标,执行 use all type Integer_Maps.Cursor;
也可能是实用的,它可以查看该类型的所有原始操作,例如 Key
和 元素
。
我通常将 use type
和 use 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;
关于import - Ada:导入不等式运算符 "/=",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66321585/