Ada - 关于 "mod 64" 类型定义的编译器警告
Ada - compiler warning about "mod 64" type definition
我需要一个有效值介于 0 到 63 之间的模块化整数类型。比如...
type Mix_Byte is mod 64;
这确实按预期编译和工作,但编译器有助于提醒我注意我可能的疏忽...
warning: 2 ** 64 may have been intended here
碰巧我根本不打算这样做,但很高兴知道编译器正在寻找 :)
它似乎只针对值 32 或 64 发出此警告,而不是 8、16 或 128。我知道 32 和 64 是常见的整数大小,在这些情况下 2 ** n
才有意义。
如何针对此特定实例消除此特定编译器警告(我想在整个项目中全局允许它,以防我在其他地方犯下真正的错误)。
我想我可以用不同的方式表达代码以便更准确地表达我的意思?
你可以试着写成2的幂:
type Mix_Byte is mod 2**6;
编辑:
或者,(基于您评论中的更多信息)
您可以使用命名数字作为模数:
Modulus : constant := 64;
type Mix_Byte is mod Modulus;
一些额外的背景信息(除了 egilhh 的回答):检查在 freeze.adb
中完成(参见 here). The warning can be enabled/disabled using -gnatw.m/.M.
(see output of gnatmake --help
). You can temporarily disable the warning by using the Warnings
pragma (see also here and here):
main.adb
procedure Main is
pragma Warnings (Off, "*may have been intended here");
type Mix_Byte_1 is mod 64;
pragma Warnings (On, "*may have been intended here");
type Mix_Byte_2 is mod 64; -- Line 7
begin
null;
end Main;
输出 (gnat)
$ gcc -c main.adb
main.adb:7:27: warning: 2 ** 64 may have been intended here
我需要一个有效值介于 0 到 63 之间的模块化整数类型。比如...
type Mix_Byte is mod 64;
这确实按预期编译和工作,但编译器有助于提醒我注意我可能的疏忽...
warning: 2 ** 64 may have been intended here
碰巧我根本不打算这样做,但很高兴知道编译器正在寻找 :)
它似乎只针对值 32 或 64 发出此警告,而不是 8、16 或 128。我知道 32 和 64 是常见的整数大小,在这些情况下 2 ** n
才有意义。
如何针对此特定实例消除此特定编译器警告(我想在整个项目中全局允许它,以防我在其他地方犯下真正的错误)。
我想我可以用不同的方式表达代码以便更准确地表达我的意思?
你可以试着写成2的幂:
type Mix_Byte is mod 2**6;
编辑:
或者,(基于您评论中的更多信息) 您可以使用命名数字作为模数:
Modulus : constant := 64;
type Mix_Byte is mod Modulus;
一些额外的背景信息(除了 egilhh 的回答):检查在 freeze.adb
中完成(参见 here). The warning can be enabled/disabled using -gnatw.m/.M.
(see output of gnatmake --help
). You can temporarily disable the warning by using the Warnings
pragma (see also here and here):
main.adb
procedure Main is
pragma Warnings (Off, "*may have been intended here");
type Mix_Byte_1 is mod 64;
pragma Warnings (On, "*may have been intended here");
type Mix_Byte_2 is mod 64; -- Line 7
begin
null;
end Main;
输出 (gnat)
$ gcc -c main.adb
main.adb:7:27: warning: 2 ** 64 may have been intended here