ada - 防止在 GNAT 中使用 Ada 202x

标签 ada ada2012

GNAT 允许以下代码,因为 Random(Generator, First, Last) 是在运行时实现的,但它不是 Ada 2012 的一部分。我能否导致它生成编译错误,因为它应该不可用?

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;

procedure Main is
   package Positive_Random is new Ada.Numerics.Discrete_Random
     (Result_Subtype => Positive);
   Generator : Positive_Random.Generator;

   -- This should fail, since function isn't part of Ada 2012.
   Value : Positive := Positive_Random.Random (Generator, 1, 10);
begin
    Put_Line (Value'Image);
end Main;

这是我的 gpr 文件:

project Default is

   for Source_Dirs use ("src");
   for Object_Dir use "obj";
   for Main use ("main.adb");

   package Compiler is
      for Switches ("ada") use ("-gnat12");
   end Compiler;

end Default;

最佳答案

在我看来,the standard way这样做是添加 a global restriction :

pragma Restrictions (No_Implementation_Identifiers);

No_Implementation_Identifiers

There are no usage names that denote declarations with implementation-defined identifiers that occur within language-defined packages or instances of language-defined generic packages.

但这在 GNAT Community Edition 2021 中不起作用(我想在 GCC 11 中也不行)。

您可以创建自定义 GNAT 运行时并删除此子程序或使用方面 Implementation_Defined 标记它以使限制生效。

关于ada - 防止在 GNAT 中使用 Ada 202x,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68605628/

相关文章:

ada - 如何在 Ada 中读取大文件?

ada - Ada 和 GNATStudio 的路径问题

ada - 在 Ada 中返回一个包含一个元素的数组

Ada:在向量上提升用户定义运算符的中间值的最佳方法是什么?

ada - 为什么 Ada 没有垃圾收集器?

ada - 相当于 Ada 中的访问器

ada - 从 Ada 程序启动可执行文件并读取结果

ada - 你如何在 Ada 中为向量实现通用排序?

java - Ada 中的未命名记录