当我使用以127结尾的范围定义我自己的类型时,编译器不会执行上限检查,这允许变量环绕并在其定义的限制之下变为负数.如果我将范围定义为126,则抛出适当的异常.我在下面列出了程序及其输出.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure GoodType is
type GOOD_TYPE is range -1..126;
package GOOD_TYPE_IO is new Ada.Text_IO.Integer_IO(GOOD_TYPE);
use GOOD_TYPE_IO;
On_Both1 : GOOD_TYPE := 120;
Index : INTEGER := 0;
begin
for Index in 120..130 loop
On_Both1 := On_Both1 + 1;
Put(Index);
Put(": ");
Put(On_Both1);
New_line;
end loop;
end GoodType;
Run Code Online (Sandbox Code Playgroud)
输出:
gnatmake -f goodtype.adb && ./goodtype
120: 121
121: 122
122: 123
123: 124
124: 125
125: 126
raised CONSTRAINT_ERROR : goodtype.adb:16 range check failed
Run Code Online (Sandbox Code Playgroud)
.
with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;
procedure BadType is
type BAD_TYPE is range -1..127;
package BAD_TYPE_IO is new Ada.Text_IO.Integer_IO(BAD_TYPE);
use BAD_TYPE_IO;
On_Both1 : BAD_TYPE := 120;
Index : INTEGER := 0;
begin
for Index in 120..130 loop
On_Both1 := On_Both1 + 1;
Put(Index);
Put(": ");
Put(On_Both1);
New_line;
end loop;
end BadType;
Run Code Online (Sandbox Code Playgroud)
输出:
gnatmake -f badtype.adb && ./badtype
120: 121
121: 122
122: 123
123: 124
124: 125
125: 126
126: 127
127: -128
128: -127
129: -126
130: -125
Run Code Online (Sandbox Code Playgroud)
GNAT当前默认禁用溢出检查(尽管此行为将在以后的版本中更改).
尝试:
gnatmake -gnato -f badtype.adb && ./badtype
Run Code Online (Sandbox Code Playgroud)
126和之间的行为差异127显然是因为前者实现为范围检查(默认情况下启用),后者实现为溢出检查(默认情况下禁用).您可以在两种情况下打印的不同错误消息中看到这一点(编译时使用-gnato:
raised CONSTRAINT_ERROR : goodtype.adb:16 range check failed
Run Code Online (Sandbox Code Playgroud)
与
raised CONSTRAINT_ERROR : badtype.adb:16 overflow check failed
Run Code Online (Sandbox Code Playgroud)