可以使用此Ada Gem 博客文章和以下示例中所示的编译指示来抑制警告:
主文件
with Ada.Unchecked_Conversion;
procedure Main is
-- Using a modular type instead of an integer type. Result is the same.
type T_U8 is mod 2**7;
pragma Warnings (Off, "types for unchecked conversion have different sizes");
function UC_Bool_To_U8 is new Ada.Unchecked_Conversion
(Source => Boolean, Target => T_U8);
pragma Warnings (On, "types for unchecked conversion have different sizes");
begin
null;
end Main;
但是,也请考虑Unchecked_Conversion
在将布尔类型转换为某些整数或模块化类型时不要使用。编译器将完全优化一个简单的if
语句,如Compiler Explorer所示:
编译器资源管理器的输入
pragma Source_File_Name (To_U8, Body_File_Name => "example.adb");
with Interfaces.C.Extensions;
function To_U8 (B : Boolean) return Interfaces.C.Extensions.Unsigned_8 with Inline is
begin
return (if B then 1 else 0);
end To_U8;
Compiler Explorer 的输出(使用编译器开关-O1
或-O2
)
_ada_to_u8:
mov eax, edi # b, tmp86
ret