如何解决Ada:导入不等式运算符“/=”
我不想 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."/=";
解决方法
您可以执行 use type Integer_Maps.Cursor;
以提供类型上的运算符的可见性。
对于容器游标,执行 use all type Integer_Maps.Cursor;
也可能是实用的,它提供对类型的所有原始操作的可见性,例如 Key
和 Element
。
我通常将 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;
,
你没有!无论如何,不是直接的。重命名相等运算符 "="
,您将免费获得不等式。
-- this line succeeds!
function "=" ( Left,Right: Integer_Maps.Cursor ) return Boolean
renames Integer_Maps."=";
这类似于覆盖运算符。请参阅 ARM 6.6,尤其是静态语义。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。