Ada 枚举类型范围
据我了解,Ada 在其枚举类型上使用基于 0 的索引。因此,在下面的 Status_Type 中,序数值从 0 到 5。
type Status_Type is
(Undefined,
Available,
Fout,
Assigned,
Effected,
Cleared);
我的问题是......以下示例的序数值是什么?它们是从 0 开始还是从超类型的序数值开始?
subtype Sub_Status_Type is Status_Type
range Available.. Effected;
subtype Un_Status_Type is Sub_Status_Type
range Fout .. Assigned;
Sub_Status_Type 序数值是从 1 到 4 还是从 0 到 3?
Un_Status_Type 序数值是从 3 到 4 还是从 1 到 2 或者从 0 到 1?
As I understand it, Ada uses 0 based indexes on its enumerated types.. So in Status_Type below, the ordinal value goes from 0 to 5.
type Status_Type is
(Undefined,
Available,
Fout,
Assigned,
Effected,
Cleared);
My question is.. what are the ordinal values for the following examples? Do they start at 0 or do they start from the ordinal value from the super type?
subtype Sub_Status_Type is Status_Type
range Available.. Effected;
subtype Un_Status_Type is Sub_Status_Type
range Fout .. Assigned;
Would Sub_Status_Type ordinal values go from 1 to 4 or from 0 to 3?
Would Un_Status_Type ordinal values go from 3 to 4 or from 1 to 2 or from 0 to 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于子类型,
'pos
将返回与基本类型相同的值(我相信分别为 1..4 和 2..3)。子类型并不是真正的新的和不同的类型,因为它们是相同的旧类型,但对其可能的值有一些额外的限制。但需要注意的是,这些值是在场景下分配的。 它们实际上对您来说应该没有什么区别,除非您使用
'val
和'pos
属性,或者您正在与在 Ada 外部(或硬件)编写的代码。另外,如果它最终确实很重要,您应该知道情况实际上要复杂得多。
'pos
和'val
不会返回编译器在生成代码时用于这些枚举值的实际位值。他们只是返回他们的“序位”;它们相对于第一个值的偏移量。默认情况下,它们通常是相同的。但是,您可以使用
for ... use
子句自行更改值分配(但不能更改序数位置分配),如下面的代码所示:For the subtypes, a
'pos
will return the same value as it would have for the base type (1..4 and 2..3 respectively, I believe). Subtypes aren't really new and different types, so much as they are the same old type, but with some extra limitations on its possible values.But it should be noted that these values are assigned under the scenes. It really should make no difference to you what they are, unless you are using the
'val
and'pos
attributes, or you are interfacing to code written outside of Ada (or to hardware).Plus, if it does end up mattering, you should know that the situation is actually much more complicated.
'pos
and'val
don't return the actual bit value the compiler uses for those enumeration values when it generates code. They just return their "ordinal position"; their offset from the first value.By default they will usually be the same thing. However, you can change the value assignments (but not the ordinal position assignments) yourself with a
for ... use
clause, like in the code below:位置号是根据基本类型定义的。因此
Sub_Status_Type'Pos(Assigned)
与Status_Type'Pos(Assigned)
相同,并且 Sub_Status_Type 的位置值从 1 到 4,而不是 0 到 3。(请注意,位置编号不受枚举表示子句的影响;对于基本类型的第一个值,它始终从 0 开始。)
顺便说一句,通过以下方式很容易找到运行一个小测试程序,打印
Sub_Status_Type'Pos(...)
的值 - 这也会告诉您不能使用保留字out
作为标识符。The position number is defined in terms of the base type. So
Sub_Status_Type'Pos(Assigned)
is the same asStatus_Type'Pos(Assigned)
, and the position values of Sub_Status_Type go from 1 to 4, not 0 to 3.(And note that the position number isn't affected by an enumeration representation clause; it always starts at 0 for the first value of the base type.)
Incidentally, it would have been easy enough to find out by running a small test program that prints the values of
Sub_Status_Type'Pos(...)
-- which would also have told you that you can't use the reserved wordout
as an identifier.是的,它使用 0 作为索引,或者更确切地说用于 类型值的位置。这不是枚举文字的值,也不是它们的二进制表示形式。
没有“序数”值。类型的值是您指定的值。您在这里混淆了“价值”、“表示”和“位置”。
Status_Type
的值为Undefined
、Available
、Out
、Assigned
、<代码>受影响和清除
。位置为 0、1、2、3、4 和 5。您可以使用这些位置来翻译
'Pos
和'Val
。表示默认为该位置,但您可以自由分配其他值(只要保持正确的顺序)。如果将其写入文件,或通过套接字发送,或将其加载到寄存器中,则会使用这些。
Yes, it uses 0 for the indexes, or rather for the position of the values of the type. This is not the value of the enumeration literals, and not the binary representation of them.
There are no "ordinal" values. The values of the type are the ones you specified. You are confusing "value", "representation", and "position" here.
The values of your
Status_Type
areUndefined
,Available
,Out
,Assigned
,Effected
, andCleared
.The positions are 0, 1, 2, 3, 4, and 5. These are what you can use to translate with
'Pos
and'Val
.The representation defaults to the position, but you can freely assign other values (as long as you keep the correct order). These are used if you write it to a file, or send it through a socket, or load it into a register..
我认为回答你的问题的最好方法是相反:
从数学上讲,子类型是其父类型的连续子集。因此,如果类型 SIZES 为 (1, 2, 3, 4, 5, 6, 7, 8) 并且您将子类型 MEDIUM 定义为 (4,5),则 MEDIUM 的第一个元素为 4。
示例:
这定义了两小组可能值,它们紧密相关:即正数是除零之外的所有自然数。
我使用此形式来说明,通过一些文本更改,我们得到以下示例:
在这里,我们正在对设备必须存在才能具有状态的直观概念进行建模,但请注意,子类型中的值是[完全]派生它们的类型中的值。
这回答了您的第二个问题:是的,枚举的元素将具有与其父类型相同的值。
至于第一个,我相信起始位置实际上是实现定义的(如果不是,那么我假设 LM 默认它为 0)。然而,您可以自由地覆盖它并提供您自己的编号,唯一的限制是枚举中前面的元素的值小于您当前分配的值[IIRC]。
I think the best way to answer your questions is in reverse:
A subtype is, mathematically speaking, a continuous subset of its parent type. So, if the type SIZES is (1, 2, 3, 4, 5, 6, 7, 8) and you define a subtype MEDIUM as (4,5) the first element of MEDIUM is 4.
Example:
This defines two small sets of possible-values, which are tightly related: namely that Positive numbers are all the Natural Numbers save Zero.
I used this form to illustrate that with a few text-changes we have the following example:
And here we are modeling the intuitive notion that a device must be present to have a state, but note that the values in the subtype ARE [exactly] the values in the type from which they are derived.
This answers your second question: Yes, an element of an enumeration would have the same value that its parent-type would.
As to the first, I believe the starting position is actually implementation defined (if not then I assume the LM defaults it to 0). You are, however free to override that and provide your own numbering, the only restriction being that elements earlier in the enumeration are valued less than the value that you are assigning currently [IIRC].