记录组件偏移量的位置属性
Position attribute for record component offset
如何获取记录组件的字节偏移量?
来自Ada Programming/Attributes/'Position
'Position is a record type component attribute. It represents the address offset of the component from the beginning of the record. The value returned is represented in storage units, which is machine-specific.
编译这段代码:
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
procedure Main is
type R is record
I : Integer;
F : Float;
end record;
begin
Put(R.I'Position); --9
Put(R.F'Position); --10
end;
结果:
main.adb:9:08: invalid prefix in selected component "R"
main.adb:10:08: invalid prefix in selected component "R"
我不知道为什么我不能编译那个?
- R.I 的偏移量应为 0.
- R.F的偏移量应该是Integer'Size/Storage_Unit.
例如,等效地查看 C++ offsetof 文档。
如果您查看参考手册 (RM 13.5.2),您会看到 R.C'Position
定义为
For a component C of a composite, non-array object R
这意味着它不适用于您代码中的类型。您需要创建一个变量:
Foo : R;
begin
Put(Foo.I'Position); --9
Put(Foo.F'Position); --10
所以wikibook中的例子似乎是错误的。
如何获取记录组件的字节偏移量?
来自Ada Programming/Attributes/'Position
'Position is a record type component attribute. It represents the address offset of the component from the beginning of the record. The value returned is represented in storage units, which is machine-specific.
编译这段代码:
with Ada.Integer_Text_IO;
use Ada.Integer_Text_IO;
procedure Main is
type R is record
I : Integer;
F : Float;
end record;
begin
Put(R.I'Position); --9
Put(R.F'Position); --10
end;
结果:
main.adb:9:08: invalid prefix in selected component "R"
main.adb:10:08: invalid prefix in selected component "R"
我不知道为什么我不能编译那个?
- R.I 的偏移量应为 0.
- R.F的偏移量应该是Integer'Size/Storage_Unit.
例如,等效地查看 C++ offsetof 文档。
如果您查看参考手册 (RM 13.5.2),您会看到 R.C'Position
定义为
For a component C of a composite, non-array object R
这意味着它不适用于您代码中的类型。您需要创建一个变量:
Foo : R;
begin
Put(Foo.I'Position); --9
Put(Foo.F'Position); --10
所以wikibook中的例子似乎是错误的。