import - Ada:导入不等式运算符 "/="

标签 import package operator-overloading ada inequality

我不想使用整个包,但我确实想导入一些功能,例如 "/=" 运算符。我知道 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 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;

关于import - Ada:导入不等式运算符 "/=",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66321585/

相关文章:

java - 使用 Javatar 在 Sublime Text 3 上导入外部 jar

sql-server-2008 - 如何使用SQL遍历文件夹中的所有文件?

Java:允许向 java.awt.image 添加自定义类吗?

python - 指定操作系统 - Ansible

ios - Xcode 6 中的 Parse 和 Facebook SDK 导入冲突 ("ParseFacebookUtils"无法识别)

javascript - 如何从其他(非项目)目录将 React Components 作为包/模块导入?

java - 如何找到 "already defined in an unnamed package"的 Java 类?

c++ - 我需要重载的最少一组运算符是什么?

c++ - 有运算符时 '=' 没有可行的重载

c++ - 基于基类方法的生成类中的运算符重载