gpt4 book ai didi

size - 如何在 Ada 中显式显示泛型类型的大小?

转载 作者:行者123 更新时间:2023-12-02 07:25:00 26 4
gpt4 key购买 nike

我正在尝试编译这段代码:https://github.com/RanaExMachina/ada-fuse

不幸的是,在构建时我得到了这个错误:

fuse-system.ads:147:04: size clause not allowed for variable length type

这似乎是个问题,因为在代码中它试图设置具有通用类型的记录的大小作为条目。这似乎是一个新错误,因为开发人员在 2.5 年前编写时没有遇到过这个问题。不幸的是,他不能在短时间内帮助我,但我必须让图书馆运转起来。然而,我对解决这个问题有点无能为力。

在我看来,基本上我必须以某种方式告诉 gnat 该类型将有多大,这与 gnat 的信念相反 - 是先验可知的:它是一种访问类型。在 record 或通用类型定义中。

相关部分是:

fuse-main.ads:
package Fuse.Main is
package IO is
new Ada.Direct_IO (Element_Type);
type File_Access is access IO.File_Type;

fuse-system.ads:
generic
type File_Access is private;
package Fuse.System is
...
type File_Info_Type is record
Flags : Flags_Type;
Fh_Old : Interfaces.C.unsigned_long;
Writepage : Interfaces.C.int;
Direct_IO : Boolean := True;
Keep_Cache : Boolean := True;
Flush : Boolean := True;
Nonseekable : Boolean := True;
Fh : File_Access;
Lock_Owner : Interfaces.Unsigned_64;
end record;
type File_Info_Access is access File_Info_Type;
pragma Convention (C, File_Info_Type);
for File_Info_Type'Size use 32*8;

我的 gnat 版本是:4.9.2-1 (debian jessie)

最佳答案

知道 File_Access 是一种访问类型,但在 Fuse.System 中,编译器不知道;它所知道的只是它是确定的并且支持赋值和相等。实际可能有数百个字节。

要告诉编译器它一种访问类型,尝试这样的事情(为了方便,我将它压缩到一个包中,在 Mac OS X 上,因此是 64 位指针大小;它编译成功):

with Ada.Text_IO;
package Fuse_Tests is

generic
type File_Type is limited private;
type File_Access is access File_Type;
package Fuse_System is
type File_Info_Type is record
Fh : File_Access;
end record;
for File_Info_Type'Size use 64;
end Fuse_System;

type File_Access is access Ada.Text_IO.File_Type;

package My_Fuse_System is new Fuse_System
(File_Type => Ada.Text_IO.File_Type,
File_Access => File_Access);

end Fuse_Tests;

或者,评论中建议的替代方案:

with Ada.Text_IO;
package Fuse_Tests is

generic
type File_Type;
package Fuse_System is
type File_Access is access File_Type;
type File_Info_Type is record
Fh : File_Access;
end record;
for File_Info_Type'Size use 64;
end Fuse_System;

package My_Fuse_System is new Fuse_System
(File_Type => Ada.Text_IO.File_Type);

-- if needed ...
subtype File_Access is My_Fuse_System.File_Access;

end Fuse_Tests;

关于size - 如何在 Ada 中显式显示泛型类型的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654885/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com