Ada 中的 Unchecked_Conversion

发布于 2024-08-30 01:10:23 字数 633 浏览 7 评论 0原文

任何人都可以让我清楚地了解 Ada 语言中未经检查的转换的使用。我已经尝试过 pdf 和 net,但所有这些都没有给我一个清晰的图片。

现在我有一小段代码如下所示:

subtype Element4_Range is integer range 1..4;
subtype Element3_Range is integer range 1..3;
subtype Myarr_Range is integer range 1..10;
type Myarr3_Type is array (Myarr_Range) of Element3_Range;
type Myarr4_Type is array (Myarr_Range) of Element4_Range;
Myarr3 : Myarr3_Type;
Myarr4 : Myarr4_Type := (1, 2, 3, 3, 1, 3, 2, 1, 2, 1);
Count_1 : Integer := 0;
Count_2 : Integer := 0;
Count_3 : Integer := 0;
*function To_Myarr3 is new Unchecked_Conversion(Myarr4_type, Myarr3_type);*

现在我的疑问是函数 Myarr3 到底做了什么?

Can anyone please make me clear about the use of unchecked conversion in the Ada language.I had tried the pdf and net but all doesn't give me a clear picture to me.

Now I have a small piece of code shown below:

subtype Element4_Range is integer range 1..4;
subtype Element3_Range is integer range 1..3;
subtype Myarr_Range is integer range 1..10;
type Myarr3_Type is array (Myarr_Range) of Element3_Range;
type Myarr4_Type is array (Myarr_Range) of Element4_Range;
Myarr3 : Myarr3_Type;
Myarr4 : Myarr4_Type := (1, 2, 3, 3, 1, 3, 2, 1, 2, 1);
Count_1 : Integer := 0;
Count_2 : Integer := 0;
Count_3 : Integer := 0;
*function To_Myarr3 is new Unchecked_Conversion(Myarr4_type, Myarr3_type);*

Now my doubt here is what does the function Myarr3 exactly do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜空下最亮的亮点 2024-09-06 01:10:23

Unchecked_Conversion 的实例将源值的字节复制到目标,而不检查这是否合理。如果值的大小不同,一些编译器会发出警告(可能取决于编译选项)。

Element3_RangeElement4_Range 均基于 Integer,并且将使用相同的字节数;因此,您的两个数组变量(Myarr3Myarr4)将需要相同数量的字节(通常为 40)。

您可以这样写

Myarr3 := To_Myarr3 (Myarr4);

,就目前情况而言,不会发生任何不好的事情,因为您用于初始化 Myarr4 的所有值都与 Element3_Range 的值一样合法。

但是,如果您这样做,

Myarr3 := To_Myarr3 (Myarr4'(1, 2, 3, 4, others => 1));

最终会得到 Myarr3(4) 包含超出 Element3_Range 合法范围的值,并且编译器没有理由相信它可能无效。这很可能会导致 Constraint_Error 的出现。

您可以自己强制检查:

if not Myarr3 (4)'Valid then
  -- handle the error case

An instantiation of Unchecked_Conversion copies the bytes of the source value to the target without checking whether this is sensible. Some compilers will warn (depending maybe on compilation options) if the values are of different sizes.

Element3_Range and Element4_Range are both based on Integer and will use the same number of bytes; so both of your array variables (Myarr3, Myarr4) will need the same number of bytes (40 typically).

You could write

Myarr3 := To_Myarr3 (Myarr4);

As it stands, nothing bad would happen because all the values you've used to initialise Myarr4 are legal as values of Element3_Range.

However, if you had

Myarr3 := To_Myarr3 (Myarr4'(1, 2, 3, 4, others => 1));

you'd end up with Myarr3(4) containing a value outside the legal range of Element3_Range, and with the compiler having no reason to believe that it might not be valid. This may well lead to Constraint_Errors down the line.

You could force a check yourself:

if not Myarr3 (4)'Valid then
  -- handle the error case
梦里寻她 2024-09-06 01:10:23

我曾经有一位同事坚持认为 unchecked_conversion 应该被命名为“Unchcked_Copy”。它所做的只是将一种类型的对象复制到另一种类型的对象中。

因此,您的 To_Myarr3 例程将接受 Myarr4 类型的数组作为参数,假装它是 Myarr3 类型的数组,然后将其中的每个元素复制到表达式的左侧。

如果你想将对象的视图从一种类型更改为另一种类型而不复制整个该死的东西,你可以在它们的访问类型上使用 Unchecked_Conversion (所以你只复制指向它们的指针)。另一种方法是使用 for object_name'address use at 将一个覆盖在另一个上(但是,它们可能都会被初始化,这可能很糟糕)。但实际上最好的方法是设计系统的类型,以便您永远不必使用 Unchecked_Conversion

I had an coworker once who instisted that unchecked_conversion should have been named "Unchcked_Copy" instead. All it does is copy an object of one type into an object of another type.

So your To_Myarr3 routine will accept as a parameter an array of type Myarr4, pretend it is an array of type Myarr3, and then copy every element in it into the left-hand side of your expression.

If you want to change your view of an object from one type to another without copying the whole darn thing around, you can instead use an Unchecked_Conversion on access types for them (so you are only copying the pointer to them). Another method is using for object_name'address use at to overlay one on the other (however, they may both get initialized, which can be bad). But really the best way is to design your system's types so that you never have to use Unchecked_Conversion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文